After having been granted an F-1 or M-1 student visa, and actually entered the United States, you can stay and pursue your studies until the expiration date shown on your I-94. That document can be accessed online from U.S. Customs & Border Protection (CBP).
(If, however, you entered via land, or you entered the U.S. before April 2013, you may have received a paper card, instead.)
There is an exception to your right to remain, however, if you violate the terms of your F-1 or M-1 visa. In that case, you will be expected to leave the U.S. right away.
COVID-19 NOTE: Normally, one of the rules of a student visa would be that you must attend classes, in person. However, in light of the pandemic, Immigration and Customs Enforcement (ICE) has said it will be "flexible with temporary adaptations" adopted by the schools sponsoring foreign students. This doesn't mean that you can invent your own rules if your school is closed or is conducting online classes. Rather, it means you should stay in close touch with your DSO about what arrangements the school is making for foreign students, and how to best maintain your status.
For M-1 vocational students, the date by which the I-94 says they must leave is usually one year from when they entered the United States.
For F-1 academic students it's a bit more complicated. Their I-94s usually say "D/S" for "Duration of Status." (See 8 C.F.R. § 214.2.)
If your I-94 has this notation, the date you complete your academic program, plus a 60-day grace period, will be when you are supposed to leave the United States.
Always remember that the program end date on your I-20 form is just an estimate. If you finish your program earlier than that date, your grace period will also begin earlier.
The only time you can stay longer as an F-1 is if you apply for optional practical training (OPT) or file a change of status application before the end of your grace period. If you are approved for OPT, you will be allowed to stay until the end date on your employment authorization document, plus a new 60-day grace period.
If you are approved for a different status, you will receive a new Form I-94 with a new date telling you when you need to leave.
(Note: The original visa stamp in your passport that you received at the consulate might have an earlier or later expiration date on it. This date, however, does not determine when you should leave the United States, only how long you can use that visa to enter the country.)
If you find that you need or want more time to complete your studies, you might be able to apply for an extension.
If you stay in the U.S. beyond the date when your studies are completed, the first consequence is that you lose your eligibility to change, extend, or adjust your status by applying directly to U.S. Citizenship and Immigration Services (USCIS). If, for example, you were to receive a U.S. job offer, you would need to leave the U.S. and visit a U.S. consulate in order to apply for a nonimmigrant visa or green card based upon it.
At the consulate, or upon any future attempt to return to the U.S., you would potentially face a hurdle to returning to the U.S. if you had already spent more than 180 days there unlawfully, as described in Consequences of Unlawful Presence in the U.S.: Three- and Ten-Year Time Bars.
Another consequence of overstaying your student status is that you must, from that point onward, go to the U.S. consulate in your home country in order to obtain any sort of U.S. visa, rather than being able to utilize the services of a U.S. consulate in another country (called "third country processing").
Once in the United States, your right to stay depends not only on when your stay expires, but whether you are maintaining your student immigration status. You can maintain your status by following all of the rules; in other words, by doing everything you agreed to do when you received the student visa or status change.
If you violate these rules, you are said to "fall out of status," meaning that your right to be in the United States disappears automatically. Your accompanying spouse and children will simultaneously lose their right to be here. You and your family could be deported and your unlawful stay in the U.S. entered onto your permanent immigration records.
The most important rules are rather simple. You must:
It's much easier to take steps to maintain status than to attempt to reinstate your status after a violation.