How it works.
Both parties have a national convention at the end of August. Each state has a republican and democrat primary or caucus to elect delegates who'll represent the state at the party's convention. The delegates are obligated to vote for the candidate who won the primary/caucus, but only for the first ballot at the convention - after that, they are free to vote as they see fit.
Republican primaries are, for the most part, winner-take-all. That is, the guy who gets the plurality (most) votes gets all the delegates.
The Democrats have most primaries where the delegates are elected on a precinct-by-precinct basis. Thus, a person can win 60% of the votes but the delegates can be split 50-50.
There are a fixed number of delegates for each party. The number needed to win the party's nomination is a known quantity. Once a candidate gets that many delegates, the rest of the primary schedule is mostly meaningless.
At the convention, the nominee is voted on and then there's about 8 weeks for the two parties to campaign for the presidency.
The president is elected by delegates to the electoral college. It's like a senate or house of representatives that is formed after the election is done, they cast their votes, and then it's dissolved.
The electors are won on a state-by-state basis in the general election, winner take all. 271 electoral votes are needed to win. In 2000, Bush won 271+ electoral votes even though Gore had more votes nationwide - the popular vote is only meaningful in the state-wide tallies.
There are rules for what happens if nobody gets 271+ votes, but I won't get into that here.
There are many 3rd parties, including the Libertarians and the Green Party. They all have their own caucuses/primaries and a convention and nominate their candidate. It's rare that any 3rd party candidate gets any electoral votes, though they might get quite a few popular votes (Ross Perot got like 19% of the vote but no electoral votes in 1992).
The rules for getting on the ballot in all 50 states are ridiculously hard, and the two parties have no incentive to ease the rules to allow the 3rd parties on the stage. It is how it is.