Gas has to travel from underground storage tanks to get a car’s gas tank. When you lift the pump handle to fill up the tank, you are actually pulling a lever to get the gas flowing. You can release the lever to stop the flow, or wait until it stops automatically. If it didn’t know when to stop, your gas tank would overflow.
The gas nozzle has a small pipe running alongside it called a venturi. When the gasoline level rises and the tip of the pipe is lower than the top level of the gas, the air pressure that keeps the gas flowing from the nozzle chokes off and the nozzle handle shuts down, stopping the gas from entering the tank. Sometimes this happens before the tank is full because the gas flows quickly and can back up and hit the venturi pipe. In this case, you can squeeze the handle again and keep going until the tank is full.