Why wasn't San Marino unified with the Kindom of Italy during the unification process? Were there plans or efforts at the time to unify the two states? Did either government at any time try to annex the other state?
According to the legend, San Marino has been de facto independent since very old times, viz. since its foundation the year 301. Then the founder San Marino famously 1 said to the mountain community: Relinquo vos liberos ab utroque homine (I free you from both men), thus freeing it from both the temporal power of the Emperor and the power of the Roman Pontifex.
Pope Niccolò IV recognized the State of San Marino in 1291, but it had been de facto governed by an assembly of the citizens, the Arengo since about the year 1000.
A written republican Constitution was approved in 1600.
This just to provide context and to witness the ancient republican history of San Marino. Now to its role in the unification of Italy.
During the Risorgimento San Marino was a safe haven for many who took part in the uprisings, including some citizens of San Marino. [...] San Marino held a fundamental role in 1849, when Giuseppe Garibaldi took shelter on the Mount Titano, when, while attempting to reach Venice, found himself surrounded by four armies in Macerata Feltria. [...] One of the Captains Regent, Domenico Maria Belzoppi I - who was himself a liberal and a member of the Carboneria [...] - graned him asylum in return for a guarantee of the independence and security of the small State.
Subsequently San Marino continued to shelter political refugees and to support the uprisings, even when Austria and the Papal State became suspicious [...].
When Italy was unified [...] the two states signed a Convention on 22 March 1862 (reviewed in 1939 and in 1971) which guarantees the independence of the Republic of San Marino, good relations and favors trade.'
(From it.wikipedia.org, my translation). The convention ratified previously existing agreements with the King of Italy, that somewhat undermine the sovereignty of San Marino, which:
[...] certain that the protecting friendship of His Majesty the King of Italy will never falter, [...] declares that will never accept that of any other power.
(Art. 1, my translation, emphasis added), however this was to some extent reverted with the last review in 1971, where San Marino still guarantees its neutrality.
It is surprisingly difficult to find free sources for detailed history of the Convention of 22 March 1862, which is the key point in the relationship between the Kingdom of Italy and the Republic of San Marino. The Convention itself is available for purchase (system error, not enough money) and consultation (system error: nowhere near where I live).
According to this source (don't be fooled by the button 'History', the relevant information is all in Italian), the small State leveraged on the friendship with Napoleon III, (who was the major foreign sponsor of Italian unification) and the desire of the Italian government to maintain his friendship in order to solve the open problems of Venice and Rome, under Austrian and Papal rule. The friendship of the Italian King himself might have been a factor, too, with many San Marino eminent people invoking the King's protection on the Republic. Even though Cavour was not enthusiast with the situation, when he was succeeded by Rattazzi an agreement became possible.
A final "conspiracy" note: because secret societies were heavily involved in the unification of Italy, they might have played a role in the signing of the Convention. This is however just a rumor.
San Marino's wikipedia page says the following:
During the later phase of the Italian unification process in the 19th century, San Marino served as a refuge for many people persecuted because of their support for unification. In recognition of this support, Giuseppe Garibaldi accepted the wish of San Marino not to be incorporated into the new Italian state.
I do have one small problem with this, based on geography.
Here's where San Marino is:
Garibaldi's expedition to unite Italy under the rule of Piedmont-Sardinia only went as far north as the northern boundary of the Kingdom of the Two Sicilies (a bit north of Naples). The territory in the middle was run by the Pope, and the "honor" of attacking the Pope's forces and taking this land fell upon the regular forces of the Kingdom of Piedmont-Sardinia, not Garibaldi. Garibaldi was really fighting for Italian unity, not for the King of P-S, and the P-S government didn't trust the guy, so they were quite anxious to get their army down to Napes ASAP and take over sovereignty of the "liberated" territory.
Here's what the map looked like in 1860:
If you compare this with the previous map, you will see that San Marino is somewhere in the border area between the brown (P-S) and the Red (Papal State). The P-S forces marched down the Adriatic coast (probably right by San Marino), defeating the Pope at Castelfidarfdo on the way, to meet up with Garibaldi's forces in Naples.
So here's what it looked like it 1861:
So it was P-S forces, not Garibaldi, that marched through during the Risorgimento. I really don't see how Garibaldi would have had any say whatsoever in the fate of San Marino way up in the north of Italy.
So the next question would be, why didn't Piedmont-Sardinia annex them, while they were in the neighborhood? The link provided in Nathan's answer gives a pretty good clue there. It appears Napolean III of France cared enough about the country's sovereignty to prevent a joint Austrian/Papal invasion seven years earlier. France was also Piedmont-Sardinia's patron. Enough so that when they needed to march through the Papal States to link up with Garibaldi, they actually sent to France for permission first. They would not have attacked another French client without permission, and their pressing goal was to get to Naples anyway, not to take every bit of territory on the peninsula.
San Marino had given a safe haven to key figures in the Italian unification movement, including Garibaldi, and apparently that's why San Marino’s desire for independence was respected. more info The link also talks about the recognition of san marino by the Kingdom of Italy in 1862.
I don't know why they did not want to unify and why the new Italian state wasn't that bothered. But it might because of san marino's remote location in the Apennines.
Support from France seem to have kept san marino safe from the papal states and Austria during the period, so it seems likely this safeguarded their independence from the kingdom of Italy as well.
These seem like good logical answers. May I add a couple of points to consider? The relationship between France and San Marino began with Napoleon Bonaparte and is easily discovered.
Napoleon III was perhaps not as dynamically republican as the above answers suggest. Especially after Magenta. He did dabble in secret societies as a youth however. And his relationship with Italian Unity does seem to have survived an assasination attempt by an 'Italian'.
King Victor Emmanuel really was travelling South in haste, so would not be too keen to take time to 'conquer' the Republic of San Marino seems the best answer. Perhaps once he got with his generals and away from Cavour, he became a bit more 'gung-ho'. We know from his letters that Cavour needed the King to get to Garibaldi before Garibaldi turned on the Papal States. Later events prove this to be a possibility (- but unlikely in the military situation which obtained in fact at the time).
For general and particular reading I recommend the Garibaldi Trilogy by Trevelyan. Also any publication with the letters of Count Camillo Cavour.