Three times this has happened to me. Amazingly it didn't happen on one where the airlock was overwhelmed three times in the first 3 days. I thought it was done after the second and ignored it for a weekend and came home to a very clogged airlock which I replaced again and moved from the basement to the kitchen. There's still a bit of foaming coming into the stem of the airlock. Just a crazy fermentation and the biggest I've ever had.
Anyway, none of the ones I had the lid pop off ended up infected, but as always Your Mileage May Vary. It's dependent on the surroundings and the stage of fermentation thus you may just be ok, I'm not gonna guarantee it though.
To fix, do as others say and use the blow-off tube method.