NPCs are more than filler or placeholders for merchants or enemy soldiers. Sometimes an NPC can become integral to the story, or more importantly, become dear to the players. Hints and ideas on how to enhance the game with well loved (or hated) NPCs
While stats and planning go a long way, I've found recently that, not surprisingly, the thing that makes memorable NPCs (good or bad, ally or villain), is the GM's roleplay and characterization. I ran an Inquisitor in 1610 that kept hounding the PCs and portrayed him consistently, in more than the black-or-white/good-evil style, and the players got to where they dreaded him, not so much because he was an enemy or rival, but because they really didn't know what to expect from him - he wound up being on their side, which surprised them, and I feel like that made him a very organic character, and I think they all remember him well.
The best way to make your NPCs memorable is to make the ones you want to stick stand out more than others. I ran a really successful campaign from level 0 to level 14 and one of the biggest reoccurring NPCs was a god that took a specialized interest in the party. He was always popping up at the worst moment asking the characters to do something for him but gods help them they always took the hook when he came about. He was rather eccentric, the dreaded Chaotic Neutral god named Gone.
As far as villains are concerned it's a good practice to make a rather annoying villain keep popping up in the worst situations. I had one villain that the party defeated at least twenty times and he always found a way to escape. When he finally met his actual demise (12-15 fights in) his master, the villain behind the scenes, animated him as a sort of low-level lich type monster. When the party killed him the last time they found out their generous benefactor was the man behind the scenes trying to thwart them.
When I find myself holding the sheet of an NPC turned group favorite, it's not always particularly powerful or stand-out-ish. Characters I thought would be short-timers somehow won the hearts of the players. Children, eccentric adults, and such. Yes, there have been some truly awe inspiring NPCs that sparked imagination, but the trick is to make real personalities for them.
The most memorable villians - well, make them truly evil. The one 3.5e campaign I ran where the enemy was a cruel empress who employed a wizard who wasn't VERY powerful, but he had a penchant for showing unsettling interest in children... They HATED this guy with one unanimous hate, and did everything in their power to destroy him. When they talk about your villian with venom and bile in their voice, as if he was a real person, you know you made him come alive.
Voices, speech affectations, and body language also help a lot. :-)
Real evil doesn't need an excuse, real evil doesn't have mommy issues (Vader), real evil does it just because they can/want to. Players love to squash true evil.
They never do need excuses, but just like in the real world, they come up with the most colorful rationalizations! Ranging from the misguided to the insane. Don't skimp on the rationalizations your villian would use for his behavior.