I don't run modules, so can't write from that standpoint, but I will say that making the world less static is the main answer.
Recently my PCs ran into three orcs set for ambush while traveling a barely-existent path in the wilderness. My next "scheduled" encounter had them finding the camp the orcs came from, where an Orc Eye of Gruumsh and two human bandits (working with the orcs) would be encountered.
During the first encounter, spells were used, so the party decided to just stop and take a long rest. I decided that in that time the remaining orc and bandits would come up to check on things and discover the resting party.
Dungeon scenarios are the same. I try to make creatures more lifelike insofar as they don't just wait in some room for eternity until a party of adventurers opens the door. So, if the party decides to rest in the dungeon, they'll have a rough time of it, while resting outside the dungeon gives the creatures that became aware of their presence in the interim, (by the generally clear signs that a party hacked their way through part of the dungeon), an opportunity to prepare defenses and gain a noticeable advantage.
Sent from my iPhone using EN World