Last year while in Ecuador I had a bag stolen with my passport in it. Here are some things you can do to prevent loss of your passport, be prepared for a loss and what to do in case your passport is lost or stolen.
Step 1: Be prepared
I grew up in scouting so try to "be prepared" per the motto. Here are a few things that I did (and some that I should have done) that made me fairly prepared.
Buy and use a hidden wallet capable of carrying your passport under your clothing. Or modify some clothing to carry and keep your passport hidden.
Scan your passport and relevant visa pages and upload to a secure online storage site that you can access from any online computer. I used Google docs to store both pdf and jpg versions.
Email yourself information like your passport number (some internet cafes have terribly slow connections and may not be able to upload a scan of your passport) and the phone numbers on the backs of any credit cards you travel with in case you need to cancel them.
Keep a photo copy of your passport and relevant visas in a different bag than your original.
Give a copy to someone that will be home and accessible that can fax you a copy if necessary.
Read about the scams and dangers in the area you are going. There are forums on Trip Advisor with some excellent information. WikiTravel often has warnings about the specific countries or cities you may be visiting.
Carry a few extra passport sized photos in different bags.
Know where the embassies are located in the country you're in. It might even be worth noting their hours of operation.
Take photos of all of the things you are bringing with you. (See my documentation.) Save these online where you can retrieve them. Have receipts for anything you might want to file a claim for.
Consider travel insurance. Pay close attention to all of the details of the policy. Also ask your homeowner insurance provider if your policy would cover theft while on a trip. (Mine did - but my deductible was $1,000 and the value was about $1,060!)
A few other things you might consider to help be prepared for theft or loss while on a trip: Request a replacement debit card from your bank but do not activate it. Keep it apart from your current card so you still have fairly easy access to cash just by calling and activating your new card when you cancel the other one.
For $10, many cellular carriers will give you a SIM card. You can activate it with them over the phone when you have a replacement phone if yours is stolen. I don't usually use my cell phone while on foreign trips but I rely on it once back in the US. Since I usually have to overnight in Seattle on my return it is handy to have once back in the country.
Step 2: File a police report
Don't panic. Really - you might have lost a few stamps in your passport and maybe some photos that are not replaceable. But you're ok and with a little bit of work you can be back to enjoying your trip.
File a police report. This is the first thing you need to do. If you are not very fluent in the primary language of the country you're in hit up an online translation service like Babelfish and type up your story. Be sure to include all items that were stolen in the police report and not just your passport. If you paid for travel insurance it may cover some of your expenses and your homeowner policy may also cover loss from theft. You'll need items to be mentioned in the report in order to file a claim.
I suggest trying to make sure that the police station's location and phone number are on the police report. Mine was typed on carbon paper with an old typewriter signed by someone and stamped a few times. No letterhead. Nothing official looking about it other than the stamp.
Step 3: File for an emergency passport
You'll now need to head to the closest embassy. Here's a list of US Embassies. Check and see when they are open. Note that they will probably be closed for both local holidays and holidays back home. In my case it was a Friday night and they were not open until Monday or Tuesday for passport services. (So I went to the beach and tried to forget about how exactly i would get home. I was even able to buy a plane ticket and fly with only my Alaska driver license!) Don't delay too long or they will be mad at you at the embassy.
You will want to have ready:
A black pen.
Form DS-64 statement regarding a lost or stolen passport.
Form DS-11 passport application.
Some places require an incident report form which you should be able to find on the specific embassy website.
Your police report original.
Print outs or photo copies of your stolen or lost passport.
Additional ID such as a birth certificate (showing citizenship), and driver license (proving ID).
At least $100 cash.
A few passport photos of yourself. (Often available for a few dollars at businesses that pop up near the embassies!)
A copy of your itinerary that shows you're planning to leave the country.
In 24 hours they had my passport ready for me. The embassy will tell you the next step. You may need to get an immigration card replacement before you try to leave the country.
Your emergency passport is valid for one year but remember that most countries require a passport that is valid for at least 6 months after the date of entry or issuance of a visa. Fortunately, you can send in your emergency passport and use a special form to receive a new full passport free of charge.
Note: This step (going to a local embassy) is impossible in Cuba for US citizens.
Step 4: Replacing your immigration card
If you were given an immigration card upon entering the country you will need one to leave. Have the embassy write down the specific name of what you need in the country's language and give you the address to the closest immigration office. Also ask how much the fee will be for the replacement immigration card.
Bring: Your new passport
A copy of your old passport
Your police report
Copy of your incoming and outgoing itinerary
You'll want your old passport copy just in case your name was entered incorrectly when you entered. One little typo and they won't be able to find you. Between your name, your passport number and your incoming flight date and number they should be able to find your record.
Pay the fee, grab your immigration card, and get back to enjoying your trip!