Before you try to cross a border, double check the exit requirements of the country you're in and the entry requirements of the country you're going to. Guidebooks are okay to check, but these things change frequently, so the best place to find this is online. For American citizens, http://travel.state.gov is your best resource. Other major countries have similar websites maintained by their foreign ministry. British citizens can find theirs at http://www.fco.gov.uk. Canadians have http://www.voyage.gc.ca. Aussies have http://www.smartraveller.gov.au. I could go on, but if you don't find yourself listed here, do a quick google search and/or complain to me. I'll see what I can do.
Never assume that rules that apply to another country's citizens also apply to you. On your government's website, find out your visa requirements. For any given border crossing you will either need a visa in advance, need the paperwork to be issued a visa at the border, or won't need a visa.
If you need a visa in advance, apply directly to the consulate of the country you're going to first. In other words, if you're in Argentina and want to go to Brazil, you go to a Brazilian consulate in Argentina. As a tip, the consulates in outlying cities are usually faster and easier to deal with than the busy embassies in national capitals. The Brazilian embassy in Buenos Aires can take weeks to issue a visa, the Brazilian consulate in Puerto Iguazu can usually do it within 24 hours. If you really have problems with the consulate, try travel and tour agencies, who will take care of it for you for a fee.
In either case, you will probably need a passport sized photo or four, cash, and your approximate dates of travel. You will also be asked for some combination of contact information within the country, and a smattering of the kinds of things you'd put on a job application (recent employer's contact info, criminal history if any, etc), as well as a smattering of other facts that you'll probably know from memory (like whether you have had contact with left-handed albino pot bellied pigs within the last ten weeks). Outside of India, some of the fields on these forms can in fact be left blank, but the more you fill out, the less likely you'll be told to go to the back of the line and finish your paperwork. Often they'll have someone going through the lines and checking this in advance, so you're unlikely to have serious problems.
The day before I apply for a visa, I usually run over to the consulate to make sure I know exactly where it is, what it looks like, and what hours the visa section operates (often they shut down at noon). You can also try to get the paperwork you need in advance, but only trust it if it comes directly from the consulate itself. That's the physical office, not the website; the forms consulates post online are almost always out of date.
Occasionally there will be a curveball. Russia is the most notorious, as of this writing requiring an official invitation from an agency within Russia. This sounds tough, but you can order one online for about $30. A quick search on google will tell you plenty (I personally recommend waytorussia.com). Also occasionally you might find people telling you that you can't apply for a visa in any country except your own. Don't believe this until you hear it from the consular employees directly in person, it's usually just a tactic to try to keep the consulate's workload down.
Speaking of which, you will hear all kinds of rumors about what kinds of crazy things you need for a visa for any given place that will surprise and scare you. Once again, don't believe them unless they come from an official government source, like your country's web portal, or an employee of their country's consulate. Before applying for my Russian visa in Beijing, I was told that I would need a Chinese residence permit by multiple hotels and tour agencies, countless travelers, several unofficial websites, and even by signs posted inside of the Russian embassy itself. When I asked the employees inside, they said I didn't need one. They also said that the rumors I had heard about stamped tickets for onward travel, proof of health insurance, and a recent negative test for HIV were false as well (though if you want something other than a tourist visa for a US citizen, that might be different).
If you need a visa but can get it at the border, life is much easier. Make sure you have all the paperwork, cash (know what currency they accept beforehand, anyone changing currency at these borders will rip you off royally), and photos you need before you arrive. Usually your passport details, a rough itinerary of where you're going (that nobody will double check after you enter), cash, and a passport-sized photo or two will suffice. You will have to fill in similar paperwork as described above, but blank spaces are usually more easily tolerated than they would be when applying for a visa in advance.
And that is how you get a visa! Like most things in travel, the more information you have, the easier it is. So make sure you know what you need before you go.