Nobody ever looks forward to a flat tire, and nobody ever says "well, that was a really good time" after having one. You can at least minimize the damage to your tire and danger to yourself, though.
Flats vs Blowouts
If you get a blowout, you'll know about it right away. Sometimes the tire can fail dramatically, with a bang as loud as a shotgun going off. Other times, it might just be a loss of air and a sudden change in your car's handling, followed by vibration, noise and a pull to one side. If it's a front tire that fails, your car might be a real handful to drive until you can get to a stop.
In either case, your first job is to pull off the road as quickly (but safely!) as you can. Don't jam on the brakes or make any sudden moves, just get over to the shoulder and the flattest, hardest surface you can find.
Turn on the emergency flashers and set the parking brake. Locate the jack and tire wrench, remove the wheel cover and break loose the lug nuts with the wrench. Find the proper spot for jacking the car and raise it no higher than necessary to remove the tire and install the spare. Put the spare on, tighten the lugs, lower the vehicle and tighten the lugs the rest of the way once the vehicle is on the ground. If your vehicle is equipped with a space-saver or temporary spare, keep your speed at 55 mph or lower and replace it with a standard tire as soon as possible.
Often it's not possible to safely jack the vehicle up and change the tire yourself -- in which case you should just take advantage of your AAA card and call for help. Also, you may as well kiss that failed tire goodbye -- even if it survives the flat without shredding the sidewall, chances are that the heat and low inflation have destroyed it internally.
Preventing a Flat
Check your tires' condition regularly and look out for foreign objects, dry rot on the sidewalls, bulges or cracks, tread separation or uneven wear.
Rotate your tires regularly -- every 5,000 miles -- to ensure even wear patterns.
Keep your front end aligned properly to prevent premature wear.
Probably the most important...check your tire inflation regularly! Under-inflated tires mean more rolling resistance, more heat buildup and more wear, which will lead to tire failure.