An election isn’t over when the polls close. It’s over when election administrators complete their postelection activities and the election results are certified. As with everything else related to elections, state law governs these postelection processes—and there are 51 models. (The states plus Washington, D.C.).
This webpage reviews the postelection processes of canvassing and certifying election results, determining voter intent and contested elections.
When the polls close on Election Day, ballot counting comes first. This may take place at the polling place or at a central location. While unofficial results may be released as they become available, official election results come much later.
Once regular ballots are accounted for, local election officials process provisional ballots--ballots cast in-person when there was doubt about the voter’s identity or eligibility to vote--and “canvass” the election. In short, they prepare results and reports for the state. The state then canvasses the election as well. See NCSL’s page on Canvass Deadlines.
After the state canvass is completed, a statewide entity certifies the results. It is this certification that provides the needed authority for swearing in newly elected officeholders. Sometimes there is a local certification as well, especially for local races. See NCSL’s page on Election.
A “contested election” does not mean a highly competitive race; instead, it means a race in which a candidate contests the outcome on legal grounds. Some states are explicit on what constitutes a contested election and when a contest can be initiated. See NCSL’s page on Contested Elections.
Some states are explicit that any paper ballots that cannot be clearly interpreted are to be counted, so long as the voter’s intent can be discerned. Without a “voter intent” law, imperfect ballots cannot be counted at all, and might be used as a reason to contest an election. See NCSL’s page on Voter Intent Laws.