Why would all of a sudden the engine light come on 2007 ford focus?

Drove the car about 2miles and parked it for a couple of hours and when I started it back up the engine light came on and didn't go out and it has never done that before and I checked the gas cap

Go get your codes read for free, just look up a shop nearby.

You have a sensor problem. Get the code(s) read. Then change the bad sensor.