With the door open, turn the key or thumbturn and watch the deadbolt. Does the bolt extend fully and retract smoothly? If it does, the lock is probably working, and the problem appears only when the door is closed. That points to the strike, frame or hinges.
If the bolt will not extend fully even with the door open, the lock itself has a fault. The cylinder, tailpiece or bolt mechanism may be worn or broken. Worn or broken parts of that kind are put right through deadbolt repair.
Try the same test with the knob or lever latch. A latch that retracts but does not spring back, or does not reach the strike, needs attention of its own, covered under door latch and strike plate repair.