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:
http://www.theworldorbust.com/wp-content/uploads/2012/06/San-Marino-in-Italy-Map-300x213.jpg
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.