At the end of the seventh book when Harry looks in to Snape's memories he finally understands that Dumbledore had asked Snape to kill him. When Dumbledore came back to Hogwarts after finding Marvolo Gaunt's ring, which Voldemort had made into a Horcrux, he was badly cursed--by, as it happens, an incurable curse. Snape tells Dumbledore that he's contained the curse for then in Dumbledore's arm, but it can't last that way forever--Dumbledore will be killed byt he curse eventually. Dumbledore asks Snape how long he has to live, and Snape tells him he might have a year left. This, then, is during the summer between Harry's fifth and sixth years at Hogwarts. Dumbledore then tells Snape that Snape will have to kill him, because killing him would be more merciful than letting him be consumed by the curse of the Horcrux. So Dumbledore would rather be killed by Snape than by the curse; but another reason he told Snape to do it is because he knew that Voldemort was trying to have Draco Malfoy kill him, but Dumbledore didn't want Malfoy to become a killer. After all, Draco was still young and he also had the advantage of being able to change sides. He didn't have to end up like his father, whereas Snape was in it for good as Dumbledore's spy.