So how do you go about writing the main bad guy? Well in my opinion the trick is to spend as much time getting to know them as a writer as you do your hero. Where writers fail is they spend all the time developing the main character, making their story arc detailed and full, and then just plonk a dastardly Villain in for them to have to overcome and defeat. This often leads to a 2D character, one who whilst may have potential to scare or be despised, never fulfills the promise.
When writing Salvation, I had two antagonists (fancy word for bad guy) in mind - the obvious Khaos Lord, and the secondary Donovan. I wanted both to have full arcs - for the reader to see them as evil and bad, but also be shown glimpses of why they were the way they were, with background on the paths they choose. Ultimately I believe there is no such thing as good and bad - often bad people do things because they believe it is right, and for their perception of the greater good. If you can write your Villain in this mindset, justifying why they do every action, it will transmit to the reader and make them connect more. You don't want the reader to go "boo and hiss" when they appear, but actually be frightened of what they could do next because they understand their mindset fully!
Writing a Villain also allows you as a writer to go to the dark places in you - we all have them, just some are more locked away than others! Writing Donovan was one part I particularly enjoyed - here is a character that has openly admitted killing women and children with his bare hands, that thinks nothing of taking a human life. But I wanted the reader to understand that if he was born merely a few centuries ago he would have been a king or god, and inside him this frustration at the lottery of when he was born was his justification for his grisly actions. Villain can also be funny; dry wit can come from them that would feel false or out of place from your hero.
So take the time to invest in your antagonists as much as your heros - what has made them like they are today; how would they react in everyday scenarios; are there chinks of redemption shining through? It will make your story fuller and the reader will thank you for it!