Visa Expiration While a Student
It's important to have a clear understanding about visas. A visa is your permission to cross a border and enter a country, so your US student visa must be valid at the time you cross the border to enter the United States. Each time you enter, you will receive a stamp in your passport that designates how long you can stay. As a student, your stamp should have D/S (Duration of Status) as the expiration date. This means that as long as you are maintaining student status you may stay in the U.S., even if your visa expires. Maintaining student status means being enrolled full time, not breaking the laws, etc.
If you don’t depart from the U.S., you can stay to complete your bachelor’s degree, move to another school to do a master’s, and stay some more to do a doctorate, etc. That is all allowed as long as you maintain status, even though your visa may expire. Once again just to be clear - after you have legally entered the US in student status you may remain in the country even if your visa expires as long as you follow the student regulations and maintain status.
Here is official State Department information explaining how long you can stay and an F-1 visa overview.
Anytime you enter the U.S. you will need a valid visa to return. If your current visa is expired, a new visa can best be obtained in your home country at the U.S. Embassy or Consulate there. A more complicated situation arises if you wish to travel to a country aside from your home country after your visa expires. In that case, US Embassy officials typically frown on issuing a visa outside your home country, and this can limit your opportunities for travel. We recommend you plan your travels to include your home country if you will need a new visa.
If your visa has expired, there are special provisions for visiting Contiguous Territory (Canada, Mexico and some Caribbean islands) for a short visit and returning directly to the U.S. Current regulations allow you to travel to contiguous territory for up to 30 days and re-enter with your passport and valid I-20 even if your visa has expired, and this procedure is called automatic revalidation. These regulations could change at any time! Even though these regulations currently exist and most students have no difficulty reentering, the officer at the Port of Entry has absolute authority to permit or deny your entry. You can cite the regulatory authority as 42.112(d) of 22 CFR if the officer questions why you are trying to enter with an expired visa. Automatic revalidation is not possible if you are a citizen of a country which is considered a state sponsor of terrorism by the United State government.