Unknown to Xerxes, the narrow pass - which Leonidas and his men were using to resist the invaders - was not the only way around Thermopylae. There were other paths, known only to locals. If the Persians knew about a rearward path, they could secretly cross over the mountains, under cover of a moonlit night, and come round Leonidas and his men. The Greeks would then be trapped.
After two days of no progress, Xerxes had a visitor. Hoping for a rich reward, Ephialtes from Malis told the king "about the track which led over the hills to Thermopylae." Herodotus records how the king used that information. With Ephialtes (whose name means "nightmare" in Greek) leading the way:
As darkness fell, the Persian king sent his best soldiers [the Immortals] to take the secret path and so come up behind the Greeks. At dawn on the third day of battle, the Greeks discovered that they had been betrayed.
Now Xerxes would be invincible. No longer could Leonidas and his men hold-off the vastly larger force. With the words of the Delphic oracle undoubtedly in his mind, Leonidas knew he must die to save Greece. But what of the others? Could they avoid a slaughter?
Leonidas, the Spartan leader, chose to fight to the end, knowing that his men could never win this battle. He told the remaining Greek soldiers to flee whilst they could, but the Spartans would show their courage and fight on.
The Three Hundred, in other words, would stay. So would the Thespians, who fought side by side with Sparta, and four hundred Thebans. Before they were surrounded, however, Leonidas dismissed the rest of his troops. They would live to tell their stories.
Knowing death was certain, Leonidas and his men fought so ferociously that their opponents "fell in heaps." The Persian commanders:
... armed with whips, urged their men forward with continual blows. Many were thrust into the sea, and there perished; a still greater number were trampled to death by their own soldiers; no one heeded the dying. For the Greeks, reckless of their own safety and desperate, since they knew that, as the mountain had been crossed, their destruction was nigh at hand, exerted themselves with the most furious valour ... (Herodotus, Histories, 7.223)
Then, at some distance from his men, Leonidas died. Furious fighting ensued, as the Spartans tried to recover their fallen leader's body. Successful - at least temporarily - at that, they made their last stand on a small hill. Standing in a circle, facing outwards, the remaining Greeks killed as many invaders as they could. Two of Xerxes' brothers died. When their weapons were destroyed, the Greeks literally fought with tooth and nail:
Here (on a little hill) they resisted to the last, with their swords, if they had them, and, if not, with their hands and teeth, until the Persians, coming on from the front over the ruins of the wall and closing in from behind, finally overwhelmed them with missile weapons [that is, with arrows and spears]. (Herodotus, Histories, 7.225)
All the final defenders died, save two. (They both had eye ailments and could no longer fight.) After the battle, Xerxes did something out-of-character. Normally respecting those who died with valor, the king dishonored Leonidas' remains. Not content with decapitating the Spartan leader, the king had the rest of his opponent's body affixed to a cross. Herodotus observes:
. . . Xerxes . . . proceeded to pass through the slain; and finding the body of Leonidas, whom he knew to have been the Lacedaemonian [Spartan] king and captain, he ordered that the head should be struck off, and the trunk fastened to a cross. This proves to me most clearly, what is plain also in many other ways - namely, that King Xerxes was more angry with Leonidas, while he was still in life, than with any other mortal. (Herodotus, Histories, 7.238)
The Greeks died at the battle of Thermopylae. Xerxes, and his army, could now march through Greece and - presumably - defeat all the city-states. But the stories of the defenders' courage at Thermopylae began to quickly spread, positively impacting the morale of other Greeks. And the delay of battle also achieved what Leonides had hoped it would. The Athenians, for example, had time to flee their city before Xerxes reached it.
The defeat at Thermopylae, in other words, is not the end of the story.