The 300 Spartans were not alone, they were supported by other Greeks.
The battle lasted for 3 days - during first two days Greeks had a very large army to defend the pass, they decided to withdraw at night from 2nd to 3rd day, after the Persians had discovered and captured the secret mountain path (due to betrayal). The bulk of the Greek army retreated, while Leonidas with at least ca. 1400 men (including all but 2 Spartiates, ca. 400 Thebans and ca. 700 Thespians) stayed to slow down the Persians.
700 Thespians and 298 Spartiates were all killed in the battle. Thebans surrendered (after some fighting) and were captured by Persians.
Initially (during first 2 days of the battle) there were 11,200 Greek soldiers at Thermopylae according to Pausanias.
7,400 in contingents listed by Diodorus Siculus
5,200 in contingents listed by Herodotus
A compilation of contingents listed by Diodorus and Herodotus gives 8,100 (because some are listed only by Diodorus and some only by Herodotus):
Peloponnesians - 4000, including:
Lacedaemonians - 1000 (including 300 Spartiates)
Mantineians - 500
Tegeans - 500
Arcadians from Orchomenus - 120
Other Arcadians - 1000
Corinthians - 400
Phliusians - 200
Mycenaeans - 80
Other Peloponnesians - 200
Other Greeks - 4100, including:
Thespians - 700
Thebans - 400
Phocians - 1000
Locrians - 1000
Melians - 1000
Greek army chose the so called Middle Gate of the Thermopylae Pass - fortified by a wall (Phocian Wall) - as their main defensive line:
==================================
I am not sure, if these numbers above refer only to hoplites or to all armed men (including light infantry). If these are only hoplites, then the total force was most likely larger. For example later during the battle of Plataea there were 33,700 hoplites and 34,500 light infantry (excluding Spartan and Thespian forces). Apart from this also 800 Athenian archers. So if numbers regarding Greek forces at Thermopylae given by Herodotus and Diodorus refer just to hoplites, then the total number could be even up to ca. 2 times higher - when we include light infantry.
We must add to this also helots of Spartiates. In the battle of Plataea there were 5000 Spartan hoplites and they had 35,000 helots (5 per each Spartiate). This would give us 1500 (300 x 5) helots at Thermopylae, but I've seen estimations which say about 900 helots at Thermopylae.
Greek casualties in the battle of Thermopylae during 3 days could be up to 4000 dead soldiers or perhaps even more.
In the last stand alone (which was on 3rd day of battle), died at least 1000 Greeks (298 out of 300 Spartiates, all of 700 Thespians, etc.).
1000 Phocians guarding the secret mountain path surely also suffered heavy losses when facing the Immortals.
When it comes to 400 Thebans - it seems that they surrendered and thus survived, but surely some of them were killed before they surrendered.
===================================
When it comes to Greek forces in the battle of Plataea, I have these numbers:
5,000 Spartan hoplites
33,700 other hoplites
35,000 helots
34,500 light infantry
800 Athenian archers
1800 Thespians
And when it comes to deployment of enemy imperial forces at Plataea: The Persians (their lines) faced 11,500 independent Greek hoplites, the Medes 8,900 hoplites, the Bactrians 3,400 hoplites, the Indians 1,300 hoplites, the Saca 2,000 hoplites, and the Medized Greeks 11,600 hoplites. This can tell us about how numerous were - in relation to each other - various contingents of the imperial army (Persians, Medes, Bactrians, Indians, etc.).
But various contingents of the Persian army could be deployed in formations of different depth (more or less ranks deep).
=========================================
Defence at Thermopylae could be initially successful thanks to extremely favourable terrain for the defenders:
Xerxes could not throw masses numbering thousands of warriors to an attack along a narrow strip / pass of land which was just some 150 meters wide, counting between the escarpment of the Kalidromo Massif and the sea. It was simply impossible to deploy so many soldiers there at once.
Below we can see the Middle Gate, to the left of it Kalidromo Massif, to the right of it was the sea (the coastline was just behind the road), in the background - but closer than buildings of the Baths - we can see the old Phocian Wall, along which the Greeks defended themselves:
View is towards Persian positions:
Zoom on the Phocian Wall (when the battle took place, it was around 1 meter high, so it provided a very good protection):
This is how it looked in Ancient times:
Satelite photos from Google Maps, we can see the scale, above - 200 feet, below - 100 meters:
It seems that at most ca. 300 - 400 men could attack the Phocian Wall in one line / row, with density of 2 men per 1 meter in each line / row.
The place was perfect for defence and it is quite an achievement for the Persians that they actually took it.
The secret narrow mountain path (discovered by Persians due to Greek traitor) - guarded by 1000 Phocians - was also easy to defend.
But it was attacked by elite Immortals and Phocians were not able to hold it.
Thermopylae is an example of offensive victory in extremely difficult terrain for attackers, while easy for defenders. The Greeks initially had at least 11,200 soldiers - in theory it should be more than enough to defend one narrow pass and one narrow mountain road, even against numerically superior enemy (who, however, were not able to exploit their superior numbers due to narrow area of the battlefield).
The last stand of Leonidas on 3rd day of battle took place below the area shown on photos and maps posted above - on Kolonos Hill:
Greek hoplites:
Persian infantry: