TL;DR: The electoral votes of each state are allocated in whatever way the state's legislature says they get allocated. Mostly.
U.S. Const. II.1.2:
Each State shall appoint, in such Manner as the Legislature thereof may direct, a Number of Electors, equal to the whole Number of Senators and Representatives to which the State may be entitled in the Congress: but no Senator or Representative, or Person holding an Office of Trust or Profit under the United States, shall be appointed an Elector.
The number of electoral votes any given state gets is constrained, and there are restrictions on who the electors who officially cast the electoral votes can be, but how the electoral votes are allocated is up to the state legislatures. A state could pass a law to have the winner of its electoral votes be decided by drawing names from a hat, or award them to the oldest living Grammy winner not constitutionally barred from the presidency, and it would probably pass constitutional muster (disclaimer: IANALAENALSICLNDIPOOTVTMTSTROAOEMIYROAISALAATGBFYYHNOTBBY2).
How this works in practice
Currently, all 50 states1 use an indirect-popular-vote system, where the results of the state (or district)'s popular vote determines which party's electors get picked (which usually translates into which candidate the state's electoral votes go to - although not always! See below):
- 48 of the 50 states, plus the District of Columbia, use a winner-take-all system, where all of the state (or district)'s electoral votes are allocated to the candidate who got the most votes in that state.
- In the remaining two states (Maine and Nebraska), the popular vote totals for each of the state's congressional districts are tallied up separately; each congressional district gets one electoral vote, which goes to the candidate who got the most votes in that congressional district, while the remaining two electoral votes go to the winner of the statewide popular vote.
There are a couple of wrinkles, however!
Firstly, a state does not have to use one of the two methods above (or even take the popular vote into account at all). Some different methods were used in the past, and others have been proposed (although not adopted):
- Early in U.S. history, the electoral votes for most states were chosen not by popular vote, but, rather, by a vote in the state legislature - no popular vote was held at all. Most of the states stopped using this method in the first quarter of the 19th century, but South Carolina's legislature continued to choose their state's electoral votes all the way up until the Civil War. (This method was also used a few times after that, when exceptional circumstances kept a popular vote from being held; the last was in 1876, when Colorado - admitted as a state just three months prior to the election - had neither the time nor the funds to arrange for a statewide popular vote.)
- Very early on, a few states used a two-round hybrid system, with the popular vote narrowing down the choices and the legislature making the final selection; this was last used, by Tennessee, in 1800.
- Up through at least 1832, some states divided themselves into electoral districts, each getting one electoral vote, which was given to the winner of that district's popular vote. (This differs from the Maine-Nebraska method in that the electoral districts would be two greater in number than the state's congressional districts, and their boundaries did not have to coincide with those of the latter.)
- In 2013, Virginia contemplated switching to a scheme similar to the Maine-Nebraska method; the difference would have been that the two statewide electoral votes would have gone to whoever won the most congressional districts within the state, rather than to whoever won the most popular votes in the state.
- Proportional-representation systems (in which the electoral votes would be divided between the candidates based on the proportion of the statewide popular vote won by each candidate) have been proposed, but not implemented.
- Finally, fifteen states (plus the District of Columbia) have adopted the National Popular Vote Interstate Compact, under which all the electoral votes of all participating states would be awarded to the winner of the nationwide popular vote. It is not yet in effect, as that would require that it be ratified by enough states to add up to at least 270 electoral votes (which has not yet happened), and its constitutionality is unknown.
Secondly, who the electors actually vote for is not necessarily decided, even once they've been chosen by the state legislatures:
- In 1944, 1956, 1960, and 1964, a slate of unpledged electors was an option on the ballot in a few Southern states; these electors would then cast their electoral votes for candidates of their own choosing. In 1960, unpledged electors actually won fourteen electoral votes in Mississippi and Alabama (they did not change the outcome of the election, however).
- More common has been the phenomenon of faithless electors, who are pledged by their state legislatures to cast their electoral votes for specific presidential and vice-presidential candidates, but cast one (or both) of their votes for someone else. Usually, there is only one faithless elector in a given election (or, more often, none at all), but the 2016 election saw seven, and the all-time record was in 1872, when one presidential candidate would have gotten 66 electoral votes had he not died prior to the Electoral College meeting; three electors voted for the dead guy anyway,6 while the other 63 cast their votes for a bunch of other people. As with the unpledged electors, faithless electors have never changed the outcome of an election.
1: Plus, since the 1961 ratification of the 23rd Amendment, the District of Columbia, which gets as many electoral votes as the state with the fewest electoral votes gets. (In practice, this has always added up to three.)
2: I am not a lawyer,3 and especially not a lawyer specialising in constitutional law, nor do I play one on TV, the movies, the stage, the radio, or any other entertainment medium; if you rely on anything I say as legal advice, and things go badly for you, you have no one to blame but yourself.
3: I have an uncle who's a lawyer, though.4
4: Though still not one specialising in constitutional law.5
5: I think.
6: Since a corpse can't be president,7 these three electoral votes were considered invalid.
7: This could change if intelligent undead were to become a thing.