Focusing on Sparta here:
Sparta was famously unfortified until the late 4th or early 3rd century BC (the exact date of its first city wall is disputed, but connected to the instability of the region in the aftermath of Alexander the Great's death). This created an unusual problem for the Spartans: an enemy that could get close enough to the city might become very difficult to repel. The Spartans therefore seem to have relied primarily on a ring of fortified sites to act as a buffer against invasions, and on the judicious use of forces to defend strong points and attack where the invaders were vulnerable.
The Spartan advantage is that the plain of the river Eurotas, in which Sparta sits, is surrounded by mountain ranges. There are only a handful of passes through these mountains, which could be guarded by garrison forts. Perhaps the most famous of these is at Sellasia, where the Spartans often tried to block entry into their territory from the north; most famously, they fought a battle there against the Macedonians in 222 BC, in which they were catastrophically defeated. But generally, it was difficult to get within 20km of Sparta from the interior of the Peloponnese if the Spartans did not want you there.
The Spartan heartland was more vulnerable from the sea. The Persians were expected to take advantage of this until their defeat at Salamis, and afterwards the Athenians regularly used this route to attack and ravage Spartan territory. The response of local communities of perioikoi was to fortify their settlements. As early as the Peloponnesian War, though Sparta itself remained unfortified, many of the smaller towns in their territory had walls, and the Spartans used these as advance bases to counterattack landing forces.
By the later 5th century BC, there was also a growing number of forts and watchtowers scattered along the coast and the mountains, similar to those found in Athens, Argos, Thessaly and elsewhere. Modern scholars argue over whether these defensive works amount to a "defensive network" deliberately designed to provide early warnings and slow down invaders; it is perhaps more likely that they served only as a refuge for local populations. But there's little doubt that these defences would have helped to make the Spartans aware if enemies were approaching.
Spartan commanders of the 4th century BC had formulated a defensive strategy based around the mountains and strongpoints on their borders, rather than on the simple principle of meeting the enemy in battle. Especially after the Spartan defeat at Leuktra in 371 BC, the latter option was regarded as both old-fashioned and unnecessarily risky. Using the terrain and choosing your battles was safer. We hear about this strategy in Xenophon's encomium of his friend, the Spartan king Agesilaos II (ruled ca. 399-359 BC):
He still kept the city safe, even though it was without walls, not going out into the open where the enemy would have total superiority, but deploying in strength where the citizens would have the advantage; for he believed that he would be surrounded on all sides if he came out into the plain, but that if he stood firm in defiles and high places, he would be master of all.
This approach was not altogether successful, though, since the Thebans (and later the Macedonians, Achaians, and Romans) would break into Spartan territory on several occasions from 370/69 BC onwards. When this happened, the Spartan response was to occupy strategic points within their urban area where troops could be gathered (the agora and the main sanctuaries) and draw up troops to block the access roads. They would then seek advantageous moments to spring ambushes (such as their attack on the Theban cavalry at the race track: Xenophon, Hellenika 6.5.30-31) or blunt the enemy advance (such as when they attacked the Theban vanguard while it was crossing the Eurotas: Diodoros 15.65.2). Sometimes they would mount desperate counterattacks, like that of Archidamos the son of Agesilaos, who charged the entire Theban army with just 100 men and drove them back up a narrow road (Xenophon, Hellenika 7.5.12).
These methods seems simple enough; they were similar to those used by other cities under attack, down to the role of local residents shouting encouragement from the rooftops of the houses. Still, perhaps due to the urgency of their situation, the Spartans tended to fight against invaders with remarkable success, and no enemy force actually entered the city until the late 3rd century.