The most common way to contest the results of an election is to request a recount; politicians can also present a case before a judge if they feel that the election was not performed properly. States that allow recounts often have restrictions on who can ask for recounts.
In general, voting officials determine if a certain vote meets the criteria necessary for a recount. In some states, recounts are automatically performed if the vote differential falls under a particular percentage-based limit; in Alabama, for example, if the difference between the winner's votes and loser's votes is less than 0.5 percent, the ballots are automatically recounted.
In other situations, someone might have to request a recount. The losing candidate, in particular, is general charged with filing a request for a recount. However, party officials might be able to make a request in certain situations as well, and 17 states allow votes to request a recount. Again, the vote differential generally needs to be within a particular margin for the official to call for a recount.
Recounts are mandatory, and seven states have no laws on the books pertaining to recounts. Judges, however, have power over elections, so concerned parties can generally file paperwork with a judge during and after the election if they believe they can demonstrate cheating or other problems with the election.