To give a simple answer to this question; Monaco survived thanks to its status as a protectorate of larger countries that others didn’t want to mess with, mostly France though, at the time of Italy’s unification, Sardinia.
Monaco had retained sovereignty throughout the medieval period, but had always done so through an alliance with a larger nation, mostly France, with a number of Monégasque lords holding military office in the kingdom despite their independence; Rainier II, for example, was Admiral of Languedoc and fought with the French at the Battle of Poitiers, and was captured by the English while escorting French merchant ships in the English Channel.
Monaco remained a subject of France until 1523, when Augustine Grimaldi — regent of Monaco to his 9-month-old nephew Lord Honoré I — severed Monaco’s relationship with France and swore Monaco’s allegiance to Spain, allowing Monaco to focus on advancing their own position under the military protection of Spain. This allegiance saw Monaco subsequently losing some of their French holdings in Savoy.
Of course, this wasn’t always a beneficial relationship for Monaco, especially in terms of tribute paid to Spain for their protection; Spanish troops also occupied the country from 1604 to 1614, during the minority rule of Honoré II, and Spain wouldn’t actually recognize Honoré as a sovereign prince until 1633.
In his adulthood, Honoré II became a critic of Spanish rule over Monaco, and in 1641 ended it completely with the signing of the Treaty of Péronne with Louis XIII of France, which established Monaco as a protectorate of France while guaranteeing the position of Honoré and his successors as sovereign rulers over Monaco.
This switch of allegiance saw Monaco lose possessions in Spain and France, though Honoré was made marquis of Les Baux and Duke of Valentinois by Louis XIII for his trouble; Monaco’s rule over the cities of Menton and Roquebrune was also re-established.
Monaco remained a protectorate of France until 1793 when it was occupied during the Revolution and incorporated into the French département of Alpes-Maritimes. Monaco’s sovereignty was re-established in 1814, though in 1815 it was made a protectorate of the Kingdom of Sardinia. Its position as a protectorate of Sardinia allowed it to remain independent during the unification of Italy, which was itself an enterprise of the Kingdom of Sardinia, which in 1861 became the Kingdom of Italy.
The country seems to have narrowly avoided incorporation into the Kingdom of Italy when in 1860 the entirety of Sardinian Nice and Savoy were ceded to France, and Monaco was made an independent protectorate of the French instead. This secured French military protection for Monaco but saw France annex the cities of Menton and Roquebrune, which accounted for 95% of Monaco’s territory, in return. Monaco has largely remained under the military protection of France ever since.
As an interesting sidenote about Monaco’s status as a tax haven; there was the precedent set for this as early as the 13th century when settlers from Genoa established a military fortification and surrounding settlement on the Rock of Monaco and offered tax exemptions to new settlers (though this is not to say that it was always a tax haven, of course).
2. Christiaan Klieger, P. 2012, The Microstates of Europe: Designer Nations in a Post-Modern World
3. ‘Principality and Diocese of Monaco’, The Catholic Encyclopedia
4. Pemberton, H. 1867, The History of Monaco: Past and Present