Primary elections are party-wide (not country-wide) elections to determine who will represent that political party in the Presidential race. Each state, like in the Electoral College, has a certain amount of electors that represent that state's party voters. After the final candidates have been selected by the political party, voters in each state that belong to each party will cast their ballots for a candidate to represent their political party. Delegates awarded to each candidate per state are based upon how much of the vote each candidate got (3).
Each party's nominee is selected based off of the results of the party primary elections. The delegates awarded to each candidate would then go to their party's convention and then vote for the candidate they are pledged to. In general, whoever wins the majority of all the delegates available in the primary elections will win the nomination.