A nightlatch is a lock that locks by itself when you push the door shut, because its latch goes into the strike plate without any need for a key. To open it from outside you need the key; from the inside the handle or knob is usually enough.
It is the typical lock on the main doors of blocks of flats, on communal doors and on many front doors. It is convenient, but it has two drawbacks: it is easy to get locked out with the keys inside, and a door that is only latched opens far more easily than one that has been locked with the key.
The advice is simple: when you go out, always turn the key, even if you will be back in a moment. If you want to replace it, see lock installation; if it has already happened to you, there is help for a door slammed shut.