Why does my latch not retract when I push or swing the door shut, but will retract when the knob/lever is used?
You may have a deadlocking latch bolt, which in addition to a normal door latch has a plunger. When this plunger is retracted (which happens when the door is closed), the spring loaded mechanism on the latch is disabled. This prevents someone from using the credit card trick to open the door from the outside, so the only way to open the door is by unlocking it and turning the knob.  In other words, it's for security.



Sample image from doorware.com, no affiliation

My guess, assuming this is an external locking door, is that this plunger is either jammed or some of the internal mechanisms that it activates have jammed.  You can unscrew the door knob and remove both sides, then unscrew the latch and pull it out of the door.  From there, try some lubricant (silicone spray and graphite powder are my preferred lubricants for locks, WD-40 will quickly dry and attract dirt).  And if it still doesn't work, it's probably time to go to the store to pickup a new doorknob.