This question is mostly about countries like Andorra, San Marino, and Liechtenstein. Why and how did these countries survive other countries expanding and the unification wars around them?
I’ll restrict to mainland Europe first, based on the examples given: most micro-states are either there or, at least by population, small island nations.
Europe once has many of what could be considered little microstates or tiny fiefdoms. Five remain independent, depending on definition. Three managed to survive the formations and conquest by of the bigger countries because they met at a nexus of competing claims by larger countries, didn’t feel like joining up some nationalistic movement, played the bigger countries against each other, and being small weren’t ever seen as urgent to invade at the risk of upsetting another power. There is a reason they lie between larger countries. Two others are enclosed by Italy, and historically relied on Italian goodwill.
Luxembourg is maybe the most complicated case, since it’s not that small and other countries certainly wanted to absorb it. It was once ruled by Austria (the Habsburgs expanded through marriage and once ruled nearly all of what is now Benelux) but after Napoleon had overrun it and then been defeated, the more modernising Metternich (the famous Austrian Chancellor) didn’t want it back, since it was their last holding that wasn’t connected to Austria proper and was landlocked, which made it a massive hassle to run. While fighting Napoleon, Prussia had invaded and occupied much of it, but Austria wasn’t as keen on expanding Prussian power and the Dutch decided that given its location and its language they should have it (Luxembourgish wasn’t considered a separate language then, and their speakers were identified as Dutch as much as German, being on a continuum of Low Franconian dialects that include Dutch and ‘German dialects’). To keep a balance of powers the Congress of Vienna awarded it to the Dutch king but as a separate grand duchy rather than part of the Netherlands, but the Prussians were still occupying it, and so they forced it to become part of the German Confederation - though not a fully integrated part.
As such it was part in and part out of Prussian rule (and later Germany) as well as the Netherlands (ruled by the Dutch king). When Belgium seceded from the Netherlands they took Lux with them, but due to its special status they agreed to split it away as part of the post war negotiations, and Lux also became independent, and later left even personal union with the NL due to different laws of succession concerning women. Germany and France came to diplomatic blows over Luxembourg but peace was considered more important and it remained a buffer state after the 1867 London Conference, though in the German Zollverein (Customs Union). Germany steamrolled over Lux in both world wars but obviously that wasn’t respected afterwards in either case.
Liechtenstein. When the Duchy of Swabia within the HRE ended back in the 13th c (gruesomely), its lands were split up and the villages that would be Liechtenstein today (Vaduz - the so-called ‘capital’, Schaan and Schellenberg, which are a formal intertwining mess on the map and basically make up the country) went directly to the Emperor. The ambitious Liechtenstein family, who mostly ruled lands within Austria (so at least two levels down within the Empire), legally needed such to get land immediately held from the Emperor to get the most coveted status of Reichsfürst or ‘imperial prince’... but weren’t able to marry into the larger princely families. But they finally managed to acquire these villages that way, and tiny as they were they technically counted. In the 1800s they found themselves caught between Prussia and Austria too in the fight for German supremacy, and lost the rest of their lands, but still had those villages (now named after their House) and took a leaf from their Swiss neighbours and declared neutrality, the one German house in a position to do so, as they were not geographically as exposed to Prussian expansion and the Habsburgs expanded on dynastic rather than nationalistic grounds. No one cared enough to violate it. They never joined Switzerland either because Switzerland was neutral for self-preservation and not about to try conquering anyone, and a prince is unlikely to want to hand his land over to a republic.
San Marino is also quite remote and hilly, without much in the way of vast resources worth the trouble, and has only been invaded a few times over many centuries. They wanted to stay independent and not join Italian unification because they had a long established republic they were very proud of (by tradition at least, dating back to the 4th century). They were allowed to because early in his expeditions they gave refuge to Italy’s national hero, Garibaldi, and the general who led much of their unification wars, and asked him to respect it, he was a man of his word, and a tiny enclosed republic hardly dashed his dreams of overall Italian independence and unification when there were foreigners and despots of larger lands to oust.
Andorra was formally the land ruled directly by the Bishops of Urgell, but as they lacked the military to defend them they appealed to the Lord Arnaud of Caboet, and later his son-in-law the Count of Foix, whose descendants through marriages eventually became kings of Navarre and then France, while the wider diocese of Urgell was ruled by Spain at a secular level. Andorra’s deal was for them both to remain head of state, and to send tribute (an established list of a few dozen food items) to both. The Bishop of Urgell and President of France are both technically princes of Andorra to this day. Except when the two were at war, they both found it more prudent to leave Andorra alone when it would mean upsetting their bigger neighbour for no gain, basically the secret to survival for most of these little countries, and this has always been the peacetime status quo.
Monaco. Centuries ago Monaco wasn’t surrounded by France, but one of a patchwork of ‘Italian’ states, many of which were controlled by Spain. They played both sides in the Franco-Spanish war in the 1600s (in the wake of the 30 Years War) and agreed to become a French protectorate and let the French kick out the Spanish they’d earlier asked to protect them, provided France mostly left them alone. During Italian reunification, the Italians (or rather the Kingdom of ‘Sardinia’) needed French support against Austria to acquire Lombardy (and the French would go on to protect the Papal States, which Italy wanted too), so in order to sweeten the deal they agreed to hand over ‘Sardinia’s’ territory around Monaco, including Nice, to the French. Incidentally this was much more objectionable to the same Garibaldi above, who was born a ‘Nizzard’, and was furious that Sardinian and then Italian prime minister Cavour ‘made him a Frenchman’. Monaco wasn’t included since the Sardinians had never acquired it - it had long protected by the French beforehand, as well as being on top of a heavily fortified rock and not really worth the trouble. The deal left Monaco with only one land border, so theoretically more at the mercy of the French, but by then France’s days of conquest in Europe were over, and Monaco chose that time to abolish all income tax, many rich people of influence now having a strong interest in it remaining independent.
The Vatican. Pope Pius XI didn’t want to pay taxes to Mussolini, or be subject to his laws, and he obviously had a very special status and a great deal of global influence, so could literally ask for his own country and get it. Mussolini wanted to keep good relations with other Catholic countries, so they signed the Lateran Treaty of 1929. The Papal States had made up the whole central belt of Italy for over a millennium until absorption within then living memory, so this wasn’t seen as too unusual.