As a general rule, you are allowed to apply for a nonimmigrant visa, such as a tourist, student, or other temporary visa, at any visa-issuing U.S. consulate or embassy. It doesn't have to be a consulate in your home country (although this is the one they assume and hope you'll go to). Applying at a third-country consulate can be convenient if you're traveling and decide to make a stop in the United States.
However, there are situations where you wouldn't want to go beyond the U.S. consulate in your home country to file your application. What's more, past visa violators may not be allowed to file their nonimmigrant visa application outside their home country.
Despite the rule allowing you to file your nonimmigrant visa application at a consulate outside your home country, how your application will actually be treated depends on which country you're from and what type of visa you're applying for.
If you live in a country where the U.S. consulate's rate of visa refusals is high, in response to the fact that many people from there overstay their visas, you may have trouble getting a visa even at a consulate in a third country.
If you're applying for an E visa (for treaty traders and investors), you may also have trouble getting an answer out of a consulate outside your home country. E visa eligibility rules are complex, and most consulates prefer that these applications be decided on by officials located in, and therefore familiar with, your home country.
The bottom line here is that consular officers, upon seeing you show up from another country, worry that you're "consulate shopping." That means looking for an easy yes that you couldn't have gotten in your home country. Countries with high rates of visa refusals and overstays include the Dominican Republic, India, China, Taiwan, and the Philippines, so anyone from one of those countries can expect their application to receive extra scrutiny, no matter where they apply.
(For more on this problem, see the article, Is It Harder to Get a Visa Coming From Certain Countries?)
If you do apply at a third-country consulate, don't be surprised if the answer takes a while. The consulate may put your application on hold, so that it can ask the U.S. consulate nearest your home to provide follow-up information.
In the end, it might be easier (if you have a choice) to simply stay home and apply for your nonimmigrant visa at the nearest U.S. consulate.
Under § 222(g) of the Immigration and Nationality Act (I.N.A.), a person who was issued a nonimmigrant visa in the past, entered the United States on the basis of that visa, and then stayed in the U.S. beyond the period of time authorized, cannot apply for further nonimmigrant visas except in the country of his or her nationality. This is referred to as the "consular shopping bar."
Interestingly, this bar does not apply to people who did not actually receive visas. That includes people who entered under the Visa Waiver Program, Canadians and others who are visa-exempt, people who entered with Mexican border crossing cards, and so on.
You'll find links to consular post websites at: www.usembassy.gov.
If you're worried that your visa application might be denied, or that you're subject to the consular shopping bar, consult with a U.S. immigration attorney first. Look for one who represents many people from your home country, and is therefore familiar with the practices and likely demands of the U.S. consulate there. The attorney can help prepare an application that allays the consulate's typical concerns.