The F-1 or M-1 student visa that's in your passport does not represent the date you must leave the U.S., but rather the dates through which you can use your visa to ENTER the United States with a valid I-20.
F-1 and M-1 students are usually admitted to the U.S. for "duration of status," meaning that you can stay in the U.S. as long as you are a student with a valid I-20. So, if the visa in your passport has run out but your I-20 is still valid and you are otherwise maintaining your status, you don't have to stop studying or leave the United States.
However, if you take a trip outside the United States and won't be returning until after the expiration date on your visa, you will need to renew the visa in order to return. This application cannot be made within the United States. All you can do within the U.S. is change or extend your "status" as a student; which will allow you to stay in the U.S. legally, but won't allow you to return after leaving.
There's no need to worry about renewing your visa until your next trip home. You might even be eligible to renew your visa without an interview, although you will still need to be in your home country and submit all the required documents.
If you do need an interview, you should schedule an appointment with the appropriate U.S. consulate at least three weeks or more before you leave the United States, depending on the wait times at the U.S. consulate in your home country.
At that time, you will need to go to your local U.S. consulate with your passport, your original I-20, your photo, a visa reciprocity fee (varies by country), and the receipt that shows you filled out State Department Form DS-160 online and paid your application fee, along with your interview confirmation page.
You should also check your passport validity period and plan for time to renew your passport if necessary, as most visa applications cannot be processed unless your passport is valid for at least six more months.
Some consulates might also ask you to submit a certified copy of your transcript and proof of enrollment from the school you are attending, as well as documents like the ones you submitted with your initial application. These include financial documents showing that you are still able to cover your tuition, fees and living expenses, and evidence of your ongoing ties to your home country.
If you are a graduate student, you should also bring a copy of your CV or resume. Each consulate might have slightly different requirements for supporting documents and passport validity, so check your visa appointment confirmation email and the consulate's website for country specific details.
In theory, you could also renew your visa in a third country such as Canada or Mexico. However, it could be more difficult for you to qualify, because the consular officers are not familiar with the documents and educational system in your home country. Also, consulates on the border may limit the availability of appointments to third-country nationals, especially during the summer. You might also need a separate visa to enter Canada or Mexico.
If you do decide to renew your visa in a third country, be aware that there is a risk that your visa application might be delayed and that you might have to remain in that country longer than you intended. This can happen if the consulate requires further security checks on your application, otherwise known as administrative processing. Such processing can take up to 60 days or more.
There is also the possibility that your visa might be denied and you would not have a way to reenter the United States.
The following checklist summarizes the forms and documents you'll need to present to an overseas U.S. consulate in order to renew your student visa. By paying careful attention before you head home, you'll ensure that you exit the U.S. without leaving anything behind.
For more information on issues relevant to your current situation, see Life in the U.S. on a Student or Exchange Visitor Visa.