One of the very best resources for ad-lib DM'ing, I've come across, thus far, is the book,
XDM: X-treme Dungeon Mastery, by Tracy & Curtis Hickman. It confirmed most of what I learned the hard way, over 30+ years of DM'ing.
Ad-lib DM'ing is a skill learned by doing. It takes time, work, and a lot of effort. It is worth it, though. A couple of my players have told me that I have ruined them for other DM's: they enjoy my style so much, that other DM's are just disappointing. I am heavy into role playing, however, so that is a huge portion of why they enjoy my games so much. I also make sure that their PC's actions are impacting their world, in small ways, at lower levels, but their actions have much more impact as their levels go up. It is a blast for all of us: I feed off of their table talk ideas, and some of them are good enough to use -- after I tweak them a bit. To do this, I have to have cultures, rituals, and customs, as well as other society factors which the players must learn, and abide by. It all works to enmesh them deeper into my imaginary world. They become a part of it, helping to shape it.
Like I said earlier in this thread, it is a delicate balance. When done properly, though, it will suck your players into your fantasy world, deep into the realm of what goes on; they will care about NPC's, what happens to them, they will seek rewards beyond mere magic items, large and small. They will want to shape the 'world' merely for the sake, and reward, of making it a better place for all NPC's, and themselves. When your players are more interested in righting a wrong, than they are in acquiring a particular magic item, or more gold, then you know you have achieved something really fun and exciting. Cheers!