I believe the first real shift for Magneto was during The Secret Wars storyline.
As per X-Men Wikia:
The characters are sorted according to their desires, and so Magneto was placed with the heroes as his desires were based on a wish to help mutants rather than the more selfish drives of the others. This surprises many of the other heroes, who still believe he is a villain, although they mostly come to accept him as an ally. Captain America even speaks in his defense on some occasions, and the Wasp develops a certain affection for him, although it is tempered by her knowledge of his past.
But the biggest change came afterwards when his Asteroid M is destroyed and he, himself, is nearly killed.
After the Secret Wars are over, Magneto is transported back to his base, Asteroid M, where the alien Warlock, traveling to Earth, collides into the asteroid, breaking it to pieces. Magneto is sent falling towards Earth and into the Atlantic Ocean, sustaining serious injuries. He is rescued by Lee Forrester, the captain of a fishing trawler. Lee helps him recuperate from his injuries and the two share a small romance. After recuperating from his injuries, Magneto is asked to aid the X-Men in battling the returned Beyonder,and Magneto stays with the X-Men even after the Beyonder is defeated. His association with the team softens his views on humanity and Magneto surrenders himself to the law to stand trial for his crimes. A special tribunal is organized. They choose to strike all charges against Magneto from prior to his "rebirth," deeming that this had constituted a figurative death of the old Magneto.
After Professor X is nearly killed in battle, he asks Magneto to take over the school and the X-Men in Uncanny X-Men #200
Xavier asks Magneto to take over his school and the X-Men, and tells him that doing so would make amends enough for his past crimes. Magneto agrees and chooses not to return to the courtroom. Instead he takes over Xavier's school under the assumed identity of Michael Xavier, Charles Xavier's cousin. Seeing him try to reform, the Scarlet Witch and Quicksilver begin accepting him as their father.