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.
(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:
(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:
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.