The gas cap seals the fuel tank and prevents fumes from venting into the atmosphere.
If you forget to insert and tighten the gas cap, your Check Engine or Service Engine light will come on. Sometimes the gas cap seal may be damaged which requires the gas cap to be replaced.
A bad gas cap can cause issues with the evaporative emissions controls as the tank is currently permanently vented to the atmosphere. Lack of fuel cap means all sorts of dust might enter the tank while you're driving. You'll also see more fuel evaporate over time, especially in hot climates.