A closet is a single square located behind a door. Often the door is a secret door.
Closets are referred to as niches in the source code.
One in seven closets (14%) are generated without doors leading to them. The only way to get to these is for a corridor to be generated adjacent to them, or to teleport or dig in. Such closets always have a scroll of teleportation inside, and a potion of object detection will reveal its presence. Every closet has either a door or a scroll of teleportation. Occasionally a closet is connected with a corridor, so if you find a scroll in a corridor next to a room before monsters could have dropped it, it is teleportation. When an inaccessible closet is generated, there is an additional 33% chance of a random item being generated there.
In Nethack 3.6.0, such inaccessible closets have a 20% chance of having iron bars generated where the door would be. If this occurs, there is a 66% chance that a human or elf corpse will be generated in the closet. As this is the only time iron bars are randomly generated, this will give away the location of the closet with a guaranteed scroll of teleportation inside.
There is sometimes writing in the dust outside. The engraving ad aerarium (from the Latin "to the treasure") indicates that the closet contains a vault teleporter, and the engraving Vlad was here indicates that there is a trap door inside. On deeper levels, a closet marked with ad aerarium may have a level teleporter instead. This is a pun on a secondary meaning of "aerarium" as sky.
Monsters can be generated inside closets just like on any other square.