A fuel cap protects your fuel supply by providing a cover for your fuel tank. They are designed to prevent fuel loss, and keep out contamination. Making sure your fuel cap is secured tightly will help decrease fuel loss due to evaporation.
But, if your check engine light comes on, or you are using fuel at a higher rate than usual, chances are your fuel cap needs replacing. Without a functioning fuel cap, gas will evaporate and reduce your fuel efficiency by 1-2%. If ignored long enough, you may eventually damage your fuel pump and filters.
From a safety standpoint, there is a low safety risk, but you could run out of gas and end up stranded.
From an environmental point of view, a leaky fuel cap will cause gas to evaporate and increase your carbon footprint.