I'm curious if the Greek spoken by someone from, say, Thebes would have been intelligible by someone from Sparta. How distinct were city states' versions of Greek? When did these dialects coalesce into one standard version of Greek?
There were a number of different dialects of Greek. In the 5th and 4th centuries, your friend from Thebes would have spoken Aeolic and the Spartan would have spoken Doric for example. Aeolic was spoken in many areas of the Northern Aegean, while Doric was spoken across vast swathes of western Greece and the Peloponnese. There were also the Ionic dialect spoken in the islands of the Aegean and on the seaboard if Asia Minor, and Attic, spoken in Attica, the region around Athens. The differences were not enormous: there were variations in the contraction of vowels, and some differences in conjugation and declension. Two speakers of different dialects would certainly have been mutually intelligible. We know this for two reasons. First is that we have many many accounts of interactions between Greeks of different regions and to my knowledge there are no references to serious language barriers. Second is that we know the Greeks were themselves aware that they shared a language, and that their common language provided a point of common identity to be shared at, for example, Panhellenic festivals and sanctuaries. As for when they coalesced into one version? This can be fairly distinctly attributed to Alexander the Great. Attic was the dialect spoken in Macedonia, and as Alexander and his army subdued first the rest of Greece and then further east their language became embedded as the common language. With the demise of the city states and the emergence of greater centralised rule Greek became more formalised and homogenised.
In terms of where the dialects came from, we're venturing into myth a little. The "Dorian invasion" and the "Ionian migration" are population movements used traditionally to explain the development and distribution of the different dialects. But be wary as these are not a good historical basis.
As a last tangential titbit, our tradition of seeing Athens (Attic) as the cultural light of Greece vs Sparta (Doric) as a macho backwater which only knows how to fight, and of course the emergence of Attic as 'proper' Greek led to Doric carrying connotations of being rustic, parochial, and uncultured. The dialect of Aberdeen and northeast Scotland was looked down on by those in central Scotland (who spoke English, or broad Scots), and as a result they called it Doric, because it was so base in comparison to sophisticated Edinburgh, and to this day that remains the official and proper name for the dialect.
Ancient Greek knew a number of different dialects. The main dialect groups were connected to the four "tribes" or peoples of Greece: Ionians, Dorians, Aeolians and Arcadians. However, even within these groups there were regional variations. Wikipedia has a nice map to show what the patchwork of dialects looked like.
Most Classical sources were written in Attic Greek, the dialect spoken by the Athenians. For this reason, modern students of Ancient Greek are actually taught Attic Greek. This dialect was a subset of Ionian Greek, but there are a number of notable differences, and this can cause some confusion when students go on to read something like Herodotus, who was from Halicarnassus and wrote in Ionian Greek. Reading texts or inscriptions from other parts of Greece (or from the Archaic period) usually requires some additional study.
Most Greeks would have been able to understand each other, but the differences would be quite noticeable, and speakers of the same dialect would always be able to tell their own. This could cause trouble in battles where men on both sides spoke the same dialect, as at Syracuse in 413 BC, where the Athenians and their Dorian allies from Argos and Corcyra fought the Dorian Syracusans:
But what hurt the Athenians as much, or indeed more than anything else, was the singing of the Paean, from the perplexity which it caused by being nearly the same on either side: the Argives and Corcyraeans and any other Dorian peoples in the army struck terror into the Athenians whenever they raised their Paean, no less than did the enemy.
-- Thucydides 7.44.6
Dorian Greek, in fact, has had a lasting influence on the way we refer to the ancient Greeks: we still talk about Dorian Sparta rather than Attic SpartĂȘ.
During the Hellenistic period, trade connections and the spread of Greek as an administrative and cultural language drove the development of koinĂȘ Greek, which was a standardised form based on the Attic dialect. However, this form of Greek in turn became subject to local differentiation, and strong regional dialects existed everywhere by the time Greece began its national unification process in the 19th century. Modern Greek is very much a top-down imposition on the various peoples of Greece, with Cyprus retaining a distinct local dialect.