As a broad 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 it might be more difficult to qualify for a nonimmigrant visa when you are already outside of your home country, applying at a third-country consulate can be convenient if you're traveling or living outside your home country and want to travel to 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 might not be allowed to file their nonimmigrant visa application outside their home country.
Despite the rule allowing one to file a nonimmigrant visa application at a consulate outside one's 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.
It is normally best to apply for your visa in the country where you have the strongest social and economic ties. For example, if you are originally from India and applying for a U.S. tourist visa, and you are also a permanent resident of Australia and have been living and working in Australia for ten years, it would be best to apply in Australia.
But, if you are a citizen of India who is in Australia on a temporary student visa and need to apply for a U.S. tourist visa, the consular officer would be evaluating your social and economic ties to India. There might be an officer there who has previously worked in India and could assist with evaluating your situation, but it is not guaranteed. Therefore, unless you have a strong economic situation or prior travel to the U.S., the officer might reject your visa and suggest that you apply in your home country.
Given this same situation above, if you were applying for a U.S. student visa to attend graduate school or a work visa, it would not be as important to be able show your ties to India, as you will be in the U.S. for a long time. Therefore,you would probably have the same chance of visa approval at either consulate.
The bottom line here is that consular officers, upon seeing you show up from another country, might worry that you're "consulate shopping," unless you have strong established ties to the country where you are applying, or some other solid reason for applying there. That means that the easy "yes" that you could have gotten in your home country might be a "no." Furthermore, applicants from countries with high rates of visa refusals and overstays can expect their application to receive extra scrutiny, no matter where they apply.
(For more on this problem, see, Is It Harder to Get a Visa Coming From Certain Countries?)
There are some countries where the U.S. has no consular services. If you are living in one of these countries, a consulate in a nearby country has probably been designated to process most visa applicants from your country. For example, many Iranian applicants apply at a U.S. consulate in Turkey, and the consular officers there are experienced in adjudicating visas for Iranian nationals. If you are in this situation, it is best apply in one of these nearby countries rather than to apply for your visa when you go on vacation somewhere far away. Many Iranians have relatives in Canada or Australia and will apply for a U.S. tourist visa while visiting relatives, but this is not ideal if you want the best chance of receiving the visa.
If you do apply at a third-country consulate, don't be surprised if the answer takes a while. The consulate might 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 apply for your nonimmigrant visa in your home country.
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 their country of nationality, unless extraordinary circumstances exist. 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 the U.S. under the Visa Waiver Program, Canadians, and others who are visa-exempt.
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.
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.