All good stories need a villain, or, more properly, an antagonist. (“Villain” is so judgmental, really — not to mention classist, since its origins lie in the Anglo-French and Old French vilain “peasant, farmer, commoner, churl, yokel” (12c.), from Medieval Latin villanus “farmhand,” from Latin villa “country house, farm.” As always, the city folks write the books.)
An antagonist is simply one who opposes the main character, also known as the protagonist. They’re often “the bad guy,” because readers like to identify with the main character, and prefer in most cases to identify with someone they can think of as “the good guy.” It doesn’t necessarily need to follow that the antagonist, in their role as “bad guy”, also has to be a bad person; all that’s required is that they present a strong and believable opponent for the protagonist to, in most cases, overcome.
What’s primarily required in an antagonist, then, is competence — or, barring that, the kind of sheer unpredictability that makes them hard for an orderly protagonist to comprehend. They can’t be insane, or at least not the kind of insanity that precludes an ability to function in the real world, and they can’t be stupid. Otherwise, they become pushovers, and pushovers are boring.
On the other hand, an antagonist shouldn’t be endowed with the kind of supernatural intelligence that lets them make elaborate plans with lots of interlocking parts that somehow never fall prey to sheer bad luck, unforeseen acts by random bystanders, or the incompetence of minions. (I see more of these than I’d like, especially in the visual media but also in books, so it’s possible that a lot of people disagree with me on this. But I’m right and they’re wrong, so there.)
And finally, an antagonist should be a fully-developed character, with virtues as well as flaws, because nobody in the real world is ever all of a piece. This means that as a writer, we have to inhabit our villains — empathize with them, if you will — as much as we do our heroes. We have to know what it is they dread when the lights go out; we have to know their petty vices and their secret good deeds; we have to know the source of their greatest sorrow and their greatest happiness; in short, we have to love them even as we bring our protagonist in to destroy them.
If we don’t do this, we risk turning our antagonists into mere mustache-twirling marionettes, when they should be human beings.
(Or aliens, or elves, or self-aware computers, as the genre requires. In short, people.)