The first gasoline pump certainly didn't exist for the purpose of fueling cars - though Karl Benz was tinkering with the idea, cars weren’t even invented yet. Created in 1885, that first pump was there to dispense fuel for stoves and kerosene lamps. It sure came in handy when cars did enter the scene, though.
The shutoff valve didn’t come along until more than 50 years later. In 1939, Olean, New York, resident Richard Corson noticed a man filling a barrel with gas and thought about what a waste of time and efficiency it was. He was sure there had to be a better way, but it wasn’t until he went to the bathroom and heard the toilet flush that the solution hit him - a butterfly float. The hands-free device would allow more than one barrel to be filled at once. Corson sketched a plan which was quickly followed by a rough prototype.
His original invention paved the way for the automatic shut-off used on most pumps today. The next time you’re trading in your savings to fill up your tank, take a second to really look at the nozzle you’re putting in the tank (don’t do this when you’re holding the trigger down). You’re probably going to notice two things: a tiny hole at the tip of the spout and a tube connected to it that runs down the interior side of the spout.
The hole is called a sensing hole; the tube connects it to a diaphragm near the shut-off valve in the nozzle.
When you squeeze the handle and precious gas starts to flow up through the spout, this creates a vacuum in that tube, keeping the diaphragm neutral. When your tank is reaching its limit, the gas is at a level high enough to cover the sensing hole. Air can’t get through the sensing hole to the vacuum, which makes the atmospheric pressure move the diaphragm, which flips a switch in the automatic shut-off device Richard Corson had a hand in creating. The switch flips, and voila, your tank is full.
Phew. Who knew keeping your shoes gas-free was so complicated?
If you’re still confused (or just want more details), here are a couple of experts:
The Straight Dope
How Stuff Works