Next time you're at the pump look for the small hole at the end of the nozzle. There's a tube from that hole up to a mechanism on the trigger that detects air pressure. When your fuel tank is empty it's full of air, just like pushing down on a gravity bong, air is forced out as you fill the tank with fuel...when the fuel gets to the top (or water in the case of a gravity bong) there is no more air coming out. When the mechanism detects no more air pressure it flips the nozzle to stop the gas from pumping.