The short answer is: It depends. There are legal ways to switch from one visa status to another with a gap in between, but only if you make sure that you never let one visa lapse without having another one in the works. We'll look closer at how to do that within this article.
As the holder of a nonimmigrant (temporary) visa, you probably know that your permitted stay in the U.S. will expire at some point. That expiration date is most likely specified on Form I-94 Arrival/Departure Record. (F-1 student visa holders are an exception, however -- their visa usually says "D/S" for duration of status, meaning they can stay in the U.S. until the completion of their studies.)
When you reach the end of your permitted U.S. stay, plus any "grace period" that may be allowed under your visa, you are expected to leave immediately. Your visa becomes automatically void. At this point, it is too late to think about taking any action to obtain a new legal status in the United States. Even if you qualify for it, the fact that your status has expired means that you cannot stay in the U.S. to apply for it. In order to avoid this result, you should plan ahead, and take action to extend or change your status (as described below) BEFORE your expiration date arrives.
Some notable exceptions to the above rule apply. For example, if you entered the U.S. on a visa and later decide to marry a U.S. citizen, you can apply for a green card (adjustment of status) even after your expiration date, without having to leave the United States first. Realize, however, that your stay will be unlawful up until the day you turn in your adjustment of status application, which means you are at risk of being caught and placed in removal proceedings.
Another important exception applies to people who have faced persecution in their home country, or fear they would be persecuted upon return. They may apply for asylum even if their visa has run out -- though they are expected to do so either within one year of U.S. entry or one year of their expiration date, depending on current policy. For the eligibility rules, see Who Is Eligible for Asylum or Refugee Protection in The U.S.?
If you act in a timely manner, you may be able to get an extension of status under your current visa (or change your status to another one, described farther below).
Most U.S. visas allow at least one extension. However, certain entrants cannot extend their stay under any circumstance. These include people who entered:
If you hold any other type of visa, you will need to look into the length of the possible extension and whether there is a maximum set on the number of years you can spend in the U.S. on that visa. For example, if you entered the U.S. on a B visa (a visitor for business or pleasure), you can extend you stay for up to six months, for a maximum stay of one year. Holders of work-based visas can usually extend their stays even longer.
Once you submit your application for an extension of status, you are permitted to remain in the U.S. until you receive a decision from U.S. Citizenship and Immigration Services (USCIS) -- even if your permitted stay expires while USCIS is considering your application.
If you qualify for another type of visa than the one you are currently on, or for a U.S. green card (lawful permanent residence), you will in most cases be able to apply for it without leaving the United States, You would do so through either a change of status or an adjustment of status application. Again, however, you will need to submit your application before your permitted stay under your old visa runs out (subject to exceptions, such as for marriage to a U.S. citizen and applying for asylum, as described above).
To change status, you fill out the same form as you would for an extension; Form I-539. See "Applying for an Extension of a U.S. Visa or Change of Status" for details.
To adjust status, the application process is more complicated, and depends on the basis for your green card eligibility. See "Adjustment of Status" for more information.
Once you submit your application for a change or adjustment of status, you are permitted to stay in the U.S. until you receive a decision from USCIS -- even if your permitted stay expires while USCIS is considering your application.