You can stay in the United States on an expired F-1 or J-1 visa as long as you maintain your student status. However, if you are returning home or traveling to a country where automatic revalidation (see question # ?) does not apply, you must have a valid visa to return to the United States.
The Department of State recommends that you apply for a visa in your home country. For more information about visa applications visit the Department of State website at http://travel.state.gov/.
You can apply in a third country for a visa, but you will not be able to return to the United States until your visa has been issued. In some cases, this could take several weeks if a background check is required. If your visa is denied, you will not be able to return to the United States. Be sure to check the Department of State website for specific information pertaining to each consulate.
If you have an expired visa and a terminated record, we strongly advise that you do not travel outside the United States until your SEVIS record shows that you are in active status. If you do travel, you may not be able to renew your visa or return to the United States.