Diagnosis
The door strike (the metal loop on the door) is misaligned with the door latch switch on the cabinet. Or the plastic latch switch is broken.
How to Fix It
Inspect the black plastic latch switch on the door frame. Push the latch mechanism with a screwdriver. You should hear a distinct click. If not, the switch is broken and needs replacement (part #5304520427). If it clicks, loosen the two screws holding the door strike on the door itself and adjust it inward slightly.
If That Didn't Work
The door hinges may be worn, letting the whole door sag. You can adjust the hinge tension, but hinge replacement is often needed for a permanent fix.
Heads Up
Never bypass the door switch to run the dryer with the door open. The drum rotates at high speed and can cause serious injury.