There are a couple of things I know of that will cause this behavior. The first is if you open the trunk from inside the car, it won't auto-lock the doors.
The second, and issue you're probably running into, is distance. The key fob proximity sensor is in the B-pillar of the passenger side. If you get out of the driver's side and stay near the door while you close it, the doors will lock. It took me about 3 months to finally figure this out.
However, if for example you get out and start moving away from the car while you're closing the door with your outstretched arm, it may not lock because the key fob is already out of the proximity sensor's range. This is pretty easy to do without thinking about it, especially if you're getting out of the car and already moving onto the next task.
FWIW, the solution is to slow down. Instead of rushing away from the car, stay near the driver's door as you close it.