Due to having always been in love with Lily Potter (neé Evans), Severus went against the Dark Lord and joined the Order of the Phoenix as their spy as soon as he heard of Voldemort's plan to go after her.
Snape, shortly after leaving Hogwarts joined Voldemort and was loyal to him for a period of about 15 years, his love for Lily remaining constant. When he realized that Voldemort took the prophecy he delivered to mean Harry (as the one to ultimately destroy the Dark Lord) ,he went to Dumbledore for help, originally only asking for Lily's protection, willing to let James (his worst enemy) and Harry die. He promised to do anything in return. Dumbledore gave him the task of being a double agent and Dumbledore did his best to protect Lily and her family. Although Dumbledore failed to protect Lily, he tried thus Snape remained loyal to him and Lily by continuing to be a double agent and by protecting Harry, putting himself in great danger.
The above person is absolutely correct of course - I just wanted to extend this by pointing out that perhaps Snape was really loyal to only Lily. He did everything he did because of his love for her...
By the way, it was NOT fifteen years it was about five.
I agree with the first guy a bit but snape isn't that old (maybe is is at that point by the end but still not there yet) Snape was loyal even to Lily but I wonder a bit if Snape wanted that protection to extend out to Harry as well? and the reason for it as well. I mean he protects Harry yes I know he's loyal to Lily but is it really just because he's related to her only? I have to disagree with the answer for the potonus charm. If it was as simple as memory then many of the character's protonus's wouldn't be as cute as of the events that have happened to all of them. Especially Harry's. Theres more in there if people would listen to the context of the movie. During the end of his memories, the surprise in dumbledorf's voice after Snape shows his protonus to him.