Before you leave on your trip you should make sure that you are aware of the visa and entry requirements to all of the countries you will be visiting. You should also plan to get at least some of your visas before you depart. Many countries will only let you stay as a tourist for a certain period of time, whether you need a visa or not. You should also be aware of these limitations, as many travelers have found themselves paying large fines or worse when they attempt to leave a country.
Sometimes it is not possible to get a visa while you are still at home due to scheduling. Sometimes you can only obtain a visa within a certain window of arriving in the country, and this window might fall during your trip. It is possible visit the embassy for those countries while you are abroad. You might have to wait longer, or pay a different amount than you would at home, but it can be done.
Sometimes you can only get a visa for a certain country from your own country. Russia, for example, has a rather challenging visa process which requires a sponsorship from a Russian travel agent. This can sometimes take weeks to arrange, and should not be left to the last minute.
Getting a visa outside of your home country can often be cheaper, especially if you're American. On our trip, we obtained a visa for Mali for about $40 while we were in Morocco. The same visa at home would have cost $131 per person. (Many countries will match your home country's visa costs, and a visa to the U.S. usually costs about $140 for foreign visitors.)
The following information is advice, but only that. Don't take our word for it, as political situations often change. Always check with official government sources for the latest visa and border information.
The United States Department of State has travel and visa information on their website for most countries in the world. The entry requirements for many countries depend on your home country, so if you are not a U.S. citizen, check with your own government.