The Goblet can only spit the fourth candidate if it is bewitched. It takes a strong charm to do this.
MAD-EYE: The hell he is. The goblet of fire is an exceptionally powerful magical object, only an exceptionally powerful conjurer could have hoodwinked it. Magic way beyond the talents of a fourth year.
It was later revealed that Barty Crouch Jr., who was disguised as Mad-Eye, did it and he knew the way to do so.
Later, near the end of the movie,
Harry Potter: It was you from the beginning. You put my name in the Goblet of Fire.
From the novel in the 17th chapter,
“Because they hoodwinked a very powerful magical object!” said Moody. “It would have needed an exceptionally strong Confundus Charm to bamboozle that goblet into forgetting that only three schools compete in the
tournament… I’m guessing they submitted Potter’s name under a fourth school, to make sure he was the only one in his category…”
So, it's clear he already knew how to bewitch the Goblet. And he put Harry's name in under a fourth school.
In 35th chapter, Moody says,
“Who put your name in the Goblet of Fire, under the name of a different school? I did. Who frightened off every person I thought might try to hurt you or prevent you from winning the tournament? I did. Who nudged Hagrid into showing you the dragons? I did. Who helped you see the only way you could beat the dragon? I did.”
The age criterion was set at 17, so Dumbledore set the charm around the goblet since, as it seems, the goblet doesn't care about the age. Harry wasn't seventeen, but Mad-Eye was older than 17, so he easily crossed the boundary.
From 12th chapter,
“Eager though I know all of you will be to bring the Triwizard Cup to Hogwarts,” he said, “the heads of the participating schools,
along with the Ministry of Magic, have agreed to impose an age restriction on contenders this year. Only students who are of age — that is to say, seventeen years or older — will be allowed to put forward their names for consideration. This” — Dumbledore raised his voice slightly, for several people had made noises of outrage at these words, and the Weasley twins were suddenly looking furious — “is a measure we feel is necessary, given that the tournament tasks will still be difficult and dangerous, whatever precautions we take, and it is highly unlikely that students be
low sixth and seventh year will be able to cope with them. I will personally be ensuring that no under-age student hoodwinks our impartial judge into making them Hogwarts champion.” His light blue eyes twinkled as they lickered over Fred’s and George’s mutinous faces. “I therefore beg you not to waste
your time submitting yourself if you are under seventeen.
So, in conclusion, Barty Crouch Jr. bewitched the goblet so it can forget that there are only three schools participating. Later, he put Harry's name in it and since he was the only candidate from that false school, the goblet spat his name out.