No matter how carefully you drive, eventually it's going to happen; you're going to get a flat tire. The most important thing, however, is to never drive on a flat tire. Always pull over immediately and put your spare tire on the vehicle. Driving on a flat tire can cause the rim to cut into the sidewall inside the the tire, severely degrading its structural integrity.
Once the damaged tire is off the car, it's time to inspect it to see if it can be repaired. It's best to take the tire to a tire shop and let them inspect it. Any tire that has gone flat should be removed from the rim to see if there is any internal damage. Unless you happen to have a tire machine at home, this is pretty much an impossible task for any home mechanic.
If there is no damage, the tire can be remounted and the leak repaired, if possible. If the leak is not easily locatable, the tire can be inflated and submerged in a tub of water. Air will seep out where the puncture or cut is and bubble to the surface when submerged, making the leak easy to spot.
If the leak is in the tread of the tire, it may be repaired using or a plug or an internal patch, depending on which is appropriate. If the puncture is in the sidewall, the tire is ruined. A tire with any sort of puncture in the sidewall, cannot, under any circumstances, be repaired.
The sidewall of a tire is thinner than the tread portion of a tire and it flexes during use. Any repairs made to the sidewall during use will leak because of this. Also, a puncture in the sidewall greatly increases the chances of a "zipper rupture," where a section of the sidewall tears in a zipper-like fashion. These sorts of blowouts are violent in nature and can cause a driver to lose control of a vehicle if they occur at a high rate of speed.
Tires are quite durable and will stand up to most objects you'll encounter in the road. However, if your tire should suffer anything more than a superficial cut on the sidewall, you should seriously consider replacing it. Definitely replace the tire if you can ever peel back the outer layer of the sidewall. The replacement cost of a tire may be high, but it's far cheaper than getting in an accident because of a tire failure.