The number and selection/election process is determined by each Party. They differ significantly.
Democrats
Democrats have two categories of delegates, pledged and unpledged. Delegates in the pledged category are required to express a presidential candidate or an uncommitted preference as a condition of election. Pledged district delegates are allocated and elected at a district level (usually the congressional district, but sometimes by state legislative district), and at-large delegates are allocated and elected at the statewide level. A third type of pledged delegate is called an “Add-on” delegate, which allows for representation by party leaders and elected officials within the state. The number of such delegates is calculated by multiplying the number of total base delegates for a state by 15%, so it is also based on the allocation factor. The add-on delegates are usually chosen in the same manner as the at-large delegates.
Democrats begin the allocation process with a base of 3,200 delegate votes, which are assigned to the states and the District of Columbia based on the allocation factor. The allocation factor is a formula that relies on the state’s Democratic vote in the previous three presidential elections and the assigned number of electoral college votes, divided by the corresponding national totals, to assign the delegates. The formula is expressed as follows:
[DonDiego apologizes for not posting the formula; it's too hard to get it to display correctly]
[The interested reader can go to page 11 of the source document referenced below.]
For example, South Dakota’s allocation factor is .00399, so its base number of delegates is: .00399 x 3,200 = 12.76, or 13 delegates. The base delegates are assigned as district level delegates (75% of the base, or 10 delegates) and at-large delegates (25% of the base, or 3 delegates). South Dakota is also entitled to two add-on delegate slots for party leaders and elected officials in the state. Delegates in these three categories are pledged delegates and required to express a presidential candidate or uncommitted preference as a condition of election. The state is also allocated a number of unpledged delegates, including four for its members of the Democratic National Committee and one for the former Senate majority leader as a Distinguished Party Leader delegate. These are the superdelegates (discussed in greater detail in the next section). Thus, the total number of delegates for South Dakota is 20, with 2 alternates, for a total delegation of 22.
Democrats also allocate delegates for six entities for which the allocation factor cannot be computed because they do not participate in presidential elections: American Samoa, Democrats Abroad, Guam, the Northern Mariana Islands, Puerto Rico, and the Virgin Islands. The party assigns at-large delegates to each entity, which also receives delegate slots for its members of the DNC, Members of Congress, and Democratic Governors.
[n.b. DonDiego has appended the "superdelegate" explanation here.]
Who Are the Superdelegates?
Among the many differences between the parties in delegate selection is the number of automatic delegate slots each party reserves for party or elected officials. Although the Republican Party designates as automatic delegates the three members of the Republican National Committee from each state, the term “superdelegate” has generally been used in reference to a group of unpledged Democratic Party delegates. During the 2012 election cycle, the media referred to the automatic RNC delegates to the convention as superdelegates as well.
The Democratic Party superdelegates are designated automatically and are not required to make known their presidential candidate or uncommitted preference, in contrast to all the other elected delegates. They include all Democratic Party Members of Congress and governors; members of the Democratic National Committee; distinguished party members, who include former Presidents and Vice Presidents, former Democratic leaders of the Senate, Speakers of the House, and minority leaders; and former chairs of the Democratic National Committee.
The superdelegates were added after the 1980 election when incumbent President James E. Carter lost to Governor Ronald Reagan in a 489-49 electoral vote landslide. The belief was that superdelegates, as party and elected leaders, could serve as a counterweight to rank and file party voters in evaluating presidential candidates.32 In this way, the superdelegates represented an effort to reduce somewhat the effect of the 1970s reforms that diminished the influence of “party elders.” Democrats increased the number of such delegates every four years since they were introduced in 1984 until the 2012 convention, for which they were slightly reduced. They made up nearly 20% of all delegates in 2008, were 14% of all delegates in 2012,33 and will be a little more than 16% of the total delegates to the Democratic convention. For Republicans, the automatic delegates to the convention make up slightly less than 7% of the total national convention. They are unbound in most states, but a few state parties bind them to vote as part of the whole delegation at the national convention.
For most of their existence, the superdelegates attracted little attention,34 but in 2008, it appeared that they might decide the contest. By February, Senator Hillary Clinton and Senator Barack Obama were so evenly matched in the fight to win delegates that the campaigns courted individually many of the 796 superdelegates, who were nearly 20% of the convention total.35 The contest was not resolved until the last events on the calendar, the June 3 primaries in South Dakota and Montana. Obama claimed victory with 1,764 pledged delegates and 438 superdelegates (2,201), as compared to 1,640 pledged delegates and 256 superdelegates for Clinton (1,896).36 A candidate needed 2,118 to win the nomination.
Republicans
Republicans use a simpler delegate allocation method than Democrats. The party assigns 10 at- large delegates to each state, as well as 3 delegates per congressional district. In addition, the party assigns bonus delegates to a state that cast its electoral votes (or a majority thereof) for the Republican nominee in the preceding election, and also assigns a single at-large delegate to states in which Republicans were elected to the following: the governor’s office, at least one half of the seats in the U.S. House of Representatives, a majority of the members of a chamber of the state legislature (if the presiding officer is a Republican elected by the chamber), a majority of members in all chambers of a state legislature (if the presiding officers are Republicans elected by each chamber), or a U.S. Senate seat (in the six-year period preceding the presidential election year). Republicans assign one alternate for each delegate.
Republicans assign at-large delegates to American Samoa, the District of Columbia, Guam, the Northern Mariana Islands, Puerto Rico, and the Virgin Islands. The District of Columbia is also eligible for bonus delegates if it cast its electoral vote (or a majority thereof) for the Republican nominee in the preceding election.
There will be 4,763 delegates and 319 alternates to the Democratic National Convention and 2,470 delegates and 2,302 alternates to the Republican National Convention in 2016. A candidate needs 2,382 Democratic delegates to secure the nomination and 1,236 delegates to secure the Republican nomination.
Ref: Congressional Research Service