As a rule, the Electoral College votes for the candidate they "should" vote for based on the popular vote in their state.
That means that when the returns come in, the networks can pretty much predict, based on how many electoral votes each state has, which candidate will get the necessary number of Electoral Votes to win (a minimum of 270 is required for a clear winner). Networks are often able to confidently predict the outcome not too long after midnight of Election Day (if that long) when there is a clear winner.
The most famous "mistake" of modern day elections was a famous headline printed by the Chicago Daily Tribune (newspaper) in the 1948 election that proclaimed, DEWEY DEFEATS TRUMAN. The most famous political PHOTO of the event shows a smiling Truman holding the newspaper up in triumph the next morning as the late returns pushed Truman's vote total over the top.
[Side note of NO value: My mother, a dyed-in-the-wool Republican all her life, confessed quietly one day, almost in a whisper, "Truman was the only Democrat I ever voted for. At the last second in the voting booth, I pulled the lever for Truman." Then her face got beet red. She had never told anyone before.]