Door locking problems almost always originate in the door latch, not the locking mechanism. The striker fails to penetrate into the door jamb because the door or the door jamb has become warped, or the screws have loosened and the plate has moved. When the door is shut, the striker doesn’t slip past the striker plate into the receiving hole in the jamb, or it goes in but doesn’t bottom out, preventing the door from locking. A homeowner with basic DIY skills and tools can fix a door that won’t latch.
Open the door. Examine the striker plate. That's the small rectangular brass plate on the jamb that the striker slips into. The striker plate is mortised into the jamb. If it is loose or has slipped out of the mortise, place it back in the mortise and tighten the screws with a Phillips screwdriver.
Shut the door and try to lock it. If it won't lock open the door. Tap the end of a chisel under the door stop on the striker side of the jamb. Pry up on it until it comes loose and remove it. Use the chisel to remove the door stop on top as well.
Remove the nails from the door stop using diagonal pliers. You can reuse the nails. Place the door stop back on the jamb. Set it back 1/4 inch from it's original location. You should see a distinct line in the paint, stain or varnish where it came off. Use this to align the door stop. Nail the side and top piece of door stop back on. The door should now shut and lock normally.
If your doorknob mechanism fails internally you can't repair it. The complete lockset, including the doorknob, needs to be changed.
If your door is badly warped, it may not seal properly even when you move the door jamb. Check the draft after fixing the door. If it's bad you might want to consider replacing the door.