A loose gas cap can illuminate the check engine light because the onboard computer detects a small emissions control leak in the system. Modern cars use very sensitive emission control components to prevent unwanted pollutants from entering the atmosphere. The onboard computer is constantly monitoring the various computerized components based on the information sent to it by the sensors that control the engine and drive train functions. When the computer detects a malfunction, it will illuminate the check engine light. Tightening the gas cap and restarting the car may be all that is required to turn off the check engine light. However, on some makes and models, the dealer must reset the check engine light.
For more info, see Possible Engine Warning Light Causes