Sauron (The Abhorred) was one of the Ainur created by Eru, before the creation of the universe. All Ainur were created good and pure, but after being granted freedom by Eru, Sauron chose not to follow the path of righteousness. He had been corrupted by Melkor, who desired to become the supreme ruler of Arda. Sauron pretended to remain loyal to the Valar, while feeding Melkor information about their actions.
By the time Sauron had left for Middle-earth, it had become clear that he was working with Melkor ( Morgoth). He deserted his service to the Valar and joined Melkor's army, where he became Melkor's lieutenant and was given command over the new stronghold of Angband. The elves declared war on Melkor and captured him, but were unable to find Sauron. Sauron had hidden himself and had begun breeding Orcs for Melkor's army.
It is revealed in The Fellowship of the Ring that during the events in The Hobbit when Gandalf departed the dwarve's company saying that, "he had urgent business away south" that he was actually part of a force attempting to push the Necromancer from his fortress Dol Guldur (Hill of Sorcery) in the south-west of Mirkwood. After his removal from Dol Guldur it was finally revealed that the "Necromancer" was indeed Sauron who had returned and had now fled back to Mordor where his preparations for a remounted conquest effort were being carried out by his servants, chiefly the nine.