Diagnosis
This is almost always a problem with the door latch mechanism. The little plastic hook on the door or the catch assembly in the frame wears out or breaks.
How to Fix It
Open the door and look at the metal latch hook on the door itself. Check for cracks or wear. More commonly, the issue is inside the door frame. You'll see a black plastic assembly that the hook engages with. Over time, the plastic pivot points inside this assembly can crack. You can often order just this catch assembly (sometimes called the door lock striker) as a spare part.
If That Didn't Work
If the latch looks physically fine, the door might be misaligned. Loosen the screws that hold the dishwasher to the cabinet (under the countertop lip) and gently re-center the whole unit so the door aligns squarely with the frame.
Heads Up
Don't try to force it or tape it shut. The machine has a safety interlock—if it doesn't sense the door is latched, it won't run the pump or heater.