For context I am perhaps most interested in the Italian city-states of the medieval and renaissance periods. Though insight into others like the Hanseatic League could be interesting.
What caused these entites to form in the first place? Did certain conditions coalesce to support their formation? Did geography protect them from being gobbled up by larger neighbours?
I find city-states interesting in general so information about other examples would be welcome too.
While there are numerous reasons and ways by which a city, or urban area, might be able to detach itself from a larger polity, a very specific set of circumstances was present in Northern Italy spurning the development of urban-centric polities.
Through the first two early-medieval Kingdoms in Italy, the Carolingian conquest, and the post-Carolingian instability, in the Italian cities clergymen, judges, and lawmakers remained seated in cities and were elected, in the Roman fashion, by colleges of prominent citizens. This collegiate process ensured the retention of strong legal systems protecting Roman-era freedoms and enfranchisement, with impersonal justice and protection of property rights, not just land rights. This was all capped off by the Mediterranean trade, which created extensive networks exchanging goods and services operated by a prosperous mercantile middle class, which wasn't willing to be taxed without a say in the matter and further pushed for institutional inclusiveness. Thus the governmental model which propped up these Italian cities was a complicated amalgam of very different groups: the urban clergy, the large and small landowners, the middle-class professionals, the artisans, and of course working poor. The autonomous Italian urban communes were strange, ad-hoc polities uniting different legal jurisdictions, prerogatives, and public will, with the end result being a patchwork of decentralized local authorities governing Northern Italy. Besides, it is not a consequence of happenstance that the center of Europe's largest empire is a peninsula characterized by dense urbanization, high agricultural productivity, and strong internal and external trade links. This meant that the two post-Roman kingdoms of Italy (both Lombard and Carolingian) had an enormous potential for internal instability: with relatively small power bases, aristocrats could easily challenge the state; while the state, such as it existed, even in times of tranquility could never adequately affirm itself. And let's not forget that Lombard Kingdom in particular never even consolidated control of the entire Italian peninsula, with entire corridors connecting major urban/productive areas held by the Eastern Empire.
The Italian states were most certainly very vulnerable when threatened by a European power, and this vulnerability would never really go away; the Italian states could, if willing to work together, fend off foreign invaders (with examples running from the twelfth century to the sixteenth) however more often than not they were more likely to draw foreign powers into their own conflicts, and were only protected by the Alps and the fact that the monarchies of Europe were usually more occupied with the consolidation of their own borders and the construction of their own state apparatus, rendering it difficult to plan the conquest integration of a foreign terretory. This might be why once the early modern states of Europe solidified, the largest and most powerful Italian states found themselves prey to Europe's great powers, and not to mention why in the post-carolingian chaos, a German monarch was breifly able to unite the German and Italian monarchies (only to have the Italian cities assert their final, and total, autonomy within a generation which you can read about here, and I'll link again below in a more contextualized list).
I've written about the development of autonomous Italian states many times in the past, I explained the main drivers behind Italian fragmentation in this answer, but if you're interested in more minutiae I also wrote more specific descriptions of the ninth, tenth and eleventh centuries, diving into some of the factors behind the emergence of Italian urban republics, and examined the sorts of conflicts that could occur within and between these cities here.
However, I'd note that by the dates traditionally associated with the renaissance, the borders areas controlled by the largest Italian cities had grown far beyond the immediate surroundings of any single city. So I'd say it's a bit incorrect to refer to the Italian States of the Renaissance as "City-States." Although they were very often the result of single cities dominating their neighbors, they were nonetheless purposes early modern polities with well-defined borders. In 1405, for example, the territories of the Republic of Venice included the important cities of Treviso, Padua, Vicenza, and Verona, each with their own history, heritage, and proud citizenry. Indeed, the cities of Padua and Verona housed a university, while the capital of the republic, Venice, did not! The Duchy of Milan was also much more than the city of Milan and its surroundings, with a large University in Pavia, and important urban centers like Lodi, Cremona, and Como.