How was Herodotus able to get accounts of the last stand of Thermopylae if the entire rear guard died alongside Leonidas? It is said he recorded the last words of the Spartans "stranger go tell Sparta we lie here obedient to her laws " and the fall of Leonidas and the Homeric battle of both sides to recover his corpse.
Firstly, your assertion that:
> It is said he recorded the last words of the Spartans "stranger go tell Sparta we lie here obedient to her laws "
That is actually the wording of an inscription that Herodotus states was erected above the graves of the Spartans. In fact, Herodotus records that three inscriptions were put there (Book VII, 228). The first was dedicated to all those who died on the Greek side:
μυριάσιν ποτὲ τῇδε τριηκοσίας ἐμάχοντο ἐκ Πελοποννάσου χιλίαδες τέτορες.
Four thousand warriors, flower of Pelops' land, Did here against three hundred myriads stand.
The second (the one you mentioned) dedicated to the Spartans:
ὦ ξεῖν’, ἀγγέλλειν Λακεδαιμονίοις ὅτι τῇδε κείμεθα τοῖσι κείνων ῥήμασι πειθόμενοι.
Go tell the Spartans, thou that passest by, that here obedient to their words we lie.
And finally one dedicated to the seer, Megistias:
μνῆμα τόδε κλεινοῖο Μεγιστία, ὄν ποτε Μῆδοι Σπερχειὸν ποταμὸν κτεῖναν ἀμειψάμενοι, Μάντιος, ὃς τότε κῆρας ἐπερχομένας σάφα εἰδώς οὐκ ἔτλη Σπάρτης ἡγεμόνα προλιπεῖν.
Here fought and fell Megistias, hero brave, slain by the Medes, who crossed Spercheius' wave; Well knew the seer his doom, but scorned to fly, And rather chose with Sparta's king to die.
Herodotus states simply that:
ἐπιγράμμασι μέν νυν καὶ στήλῃσι, ἔξω ἢ τὸ τοῦ μάντιος ἐπίγραμμα, Ἀμφικτύονες εἰσὶ σφέας οἱ ἐπικοσμήσαντες· τὸ δὲ τοῦ μάντιος Μεγιστίεω Σιμωνίδης ὁ Λεωπρέπεος ἐστὶ κατὰ ξεινίην ὁ ἐπιγράψας
The inscriptions and the pillars were set there in their honour by the Amphictyons, except the epitaph of the diviner Megistias; that inscription was made for him for friendship's sake by Simonides son of Leoprepes.
As to your main question,
> How was Herodotus able to get accounts of the last stand of Thermopylae if the entire rear guard died alongside Leonidas?
The answer is simply that the entire rear-guard did not die.
Many of the Thebans surrendered to the Persians when they got the chance:
As for the Thebans, whose general was Leontiades, they were for a while with the Greeks and constrained by necessity to fight against the king's army; but as soon as they saw the Persians gaining the upper hand, then, when the Greeks with Leonidas were pressing towards the hillock, the Thebans separated from them and drew nigh to the foreigners, holding out their hands and crying that they were the Persians' men and had been among the first to give earth and water to the king.
- Book VII, 233 (my emphasis)
Herodotus states that they were branded with Xerxes mark, but - although it is not explicitly stated - they presumably survived to tell their version of events:
... most of them were branded by Xerxes' command with the king's marks, from their general Leontiades downwards.
Furthermore, there would have been survivors of the invasion from the Persian side who had been present at Thermopylae .
Remember that Herodotus was born c 484 BCE, and the Battle of Thermopylae was fought in 480 BCE, just a few years later. He would have been able to ask the survivors of the battle directly, or speak to others who had done so when he was too young.
Although Herodotus' account is all that has come down to us, the story would certainly have been a part of the oral tradition of the people of Greece who lived through the Persian invasion. That traditional version would probably have been the version that he recorded in his Histories.
According to Herodotus, "Stranger go tell..." is not the last words by a Spartan, but the inscription on the monument which was installed after the battle. (Book VII, 228). It is clear that Herodotus could see this monument.
Concerning other details, the battle happened during the life time of Herodotus, though he was very young. So he could interview the survivors, or people who knew the survivors, and also people who fought on the Persian side (many of them were Greeks).