Answer:
Electoral votes are allocated among the States based on the Census. Every State is allocated a number of votes equal to the number of senators and representatives in its U.S. Congressional delegation—two votes for its senators in the U.S. Senate plus a number of votes equal to the number of its Congressional districts.
Under the 23rd Amendment of the Constitution, the District of Columbia is allocated three electors and treated like a State for purposes of the Electoral College.
Each State (which includes the District of Columbia for this discussion) decides how to appoint its electors. Currently all States use the popular vote results from the November general election to decide which political party chooses the individuals who are appointed.
Allocation within each State
All States, except for Maine and Nebraska have a winner-take-all policy where the State looks only at the overall winner of the state-wide popular vote. Maine and Nebraska, however, appoint individual electors based on the winner of the popular vote for each Congressional district and then 2 electors based on the winner of the overall state-wide popular vote.
Even though Maine and Nebraska don't use a winner-take-all system, it is rare for either State to have a split vote. Each has done so once: Nebraska in 2008 and Maine in 2016.