First off it wasn't just the 300 Spartans it was around 7 to 8 thousand strong army of allied Greek city states that battled at Thermopylae for about 7 to 8 days inflicting heavy physical and moral damage on the Persians however in the end King Leonidas ordered a full retreat because a local had betrayed them and told the Persians of a mountain pass that they could use to flank the Greeks, however King Leonidas and his rear guard of around 300 Spartans, 700 Thespians and 400 Thebans as well as an odd assortment of other troops stayed to halt the advance. In the end King Leonidas and his rear guard were slaughtered but they did buy enough time for the rest of the forces to fully retreat and King Leonidas death galvanized the rest of Sparta to rally with the other Greek city states to defeat the Persians.