That is relatively new, though. As far as I remember, in the "old days" as a foreigner you got those cardboard slips stapled into your passport which were removed again when you left the country. Today they just do this electronically and keep track of who is in the country by receiving your passport information from the airlines. (You can even check your arrival and departure history online: https://i94.cbp.dhs.gov/I94/#/history-search)