In an election, how can we tell the certainty that a candidate will be the winner? There was a general election where I live yesterday and the television network started calling out winners long before all ballots were opened.
They turned out right on all accounts, and I'm not really surprised they did. I know that statistics are absolutely viable. Still, I'm curious. Assuming:


*

*we have opened $i$ out of $j$ ballots;

*we have $n$ candidates whose current scores are $c_1, c_2, c_3, ... c_n$;


How can we calculate the certainty with which the leading candidate is the winner?
 A: The main difficulty in practice is not the statistical uncertainty that a fluke streak of luck would have given one candidate more votes.  The main difficulty, by an order of magnitude or more, is that the ballots which have been opened are almost never an unbiased sample of the votes cast. If you ignore this effect, you get the famous error "Dewey Defeats Truman," which occurred with a large biased sample.
In practice, voters who favor one candidate versus another are not equally distributed by region, by whether they work during the day, or by whether they would be deployed overseas hence would vote by absentee ballots. These are not small differences.
I think what news organizations do now is to break the population into groups and use the results to estimate how each group voted (including turnout). These may be based on models and prior assumptions based on previous elections, not just the data from this election. These may not take into account oddities such as the butterfly ballots of Palm Beach.
A: In survey sampling the standard error of the estimate of proportion is needed.  It depends more on i than j.  Also it requires that the i opened ballots were selected at random. If p is the true final proportion for candidate A, then the variance of the estimate is 
$$\frac{(1-\frac{i}{j})p(1-p)}{i}$$
The quantity $(1-\frac{i}{j})$ is called the finite population correction factor.  To estimate this variance the usual estimate for p is substituted for p in the formula. The standard error is gotten by taking the square root. In predicting a winner the pollster might use the estimate plus or minus 3 standard errors. If 0.5 is not contained in the interval, then Candidate A is declared the winner if 0.5 is below the lower limit, or his opponent is declared the winner if 0.5 is above the upper limit. Of course this only says with very high confidence who the winner will be in the event that 0.5 is outside the interval. The confidence level is 0.99 if three standard errors is what you use (based on the normal approximation to the binomial). If 0.5 is inside the interval no one is declared the winner and the pollster waits for more data to accumulate.
In making a projection the pollsters can select a stratified random sample from the accumulated votes to avoid potential bias that mmay occur if one looks at all the counted ballots.  The problem with looking at all accumulated votes is that certain precincts complete counting over others and they may not be representative of the population.
The article here provides good coverage of the problem and numerous references.
It has been pointed out that accumulated votes can provided biased estimates of proportions because either the precincts that have yet to report are precincts that tend to favor the party with the candidate that is trailing or the absentee ballots are likely to favor the candidate that is trailing and those votes get counted last.  The sophisticated pollsters like Harris and Gallup do not fall into such traps.  The simple analysis of constructing confidence intervals based on accumulated votes that I have outlined is only one factor that is used.  These pollsters have a great deal more information at their disposal.  They have polls that were taken shortly before the election and they have the voting patterns of all the precincts and absentee votes taken in elections in recent past years.
So if there are clear biases that could swing a close election in the opposite direction the pollsters will recognize this and hold off projecting a winner.
In the US absentee ballots come predominantly from the military overseas and college students who are at school away from home.  While the military may tend to be more conservative and likely to vote Republican, the colleage students tend to be more liberal and likely to vote Democratic.  All these considerations are taken into account.
The care and sophistication of modern polling is the reason that gross errors such as the Literary Digest poll of 1936 or the Chicago newspaper's premature concession of the 1948 election to Dewey have not occurred since then.
