Hagrid is expelled from Hogwarts as a student, but he said that the courtesy of Dumbledore let him stay as a gamekeeper. Why did Hogwarts expel him?
In The Chamber of Secrets, it is revealed that Hagrid was expelled because it is suspected that he is responsible for the death of a Muggle born witch.
Now known as the ghost "Moaning Myrtle".
This death was the result of the creature that resides in the Chamber of Secrets, which had been opened by the "Heir of Slytherin":
Tom Riddle, aka Lord Voldemort.
However, Hagrid was framed, because the headmaster at the time, Armando Dippet, was convinced that the death was instead caused by Hagrid's "pet":
Aragog, the Acromantula.
The reason that he was expelled and allowed to remain on as gamekeeper is that the existence of the Chamber of Secrets was not generally known. Cuthbert Binns says as much in The Chamber of Secrets:
But these honest facts have been obscured by the fanciful legend of the Chamber of Secrets. The story goes that Slytherin had built a hidden chamber in the castle [...]
And Hagrid is not believed to have intentionally killed the victim, just that he brought dangerous creatures into the castle:
I don't think you meant to kill anyone. But monsters don't make good pets. I suppose you just let it out for exercise and -
This indirect association with the death, combined with Dumbledore's intervention, meant that he was only expelled, and not given a more serious punishment. The school administration treated it as an accident.
Only the Transfiguration teacher, Dumbledore, seemed to think Hagrid was innocent. He persuaded Dippet to keep Hagrid and train him as gamekeeper. Yes, I think Dumbledore might have guessed. Dumbledore never seemed to like me as much as the other teachers did…