The tree was planted the year a werewolf named Remus Lupin arrived at the school (c. 1971). Although most headmasters would never have allowed a werewolf to become a student at Hogwarts, the new headmaster, Albus Dumbledore, thought that as long as certain precautions were taken there was no reason why Lupin shouldn't be allowed to attend lessons at the school. A house, later called the Shrieking Shack, was built in the nearby town of Hogsmeade; connected to Hogwarts through a secret passage. The Whomping Willow was planted over the entrance to this secret passage, both to hide the passage, and to deter anyone who might otherwise find the entrance. Lupin was smuggled to the house through this tunnel each month at the full moon. Students used to play a game where they tried to get close enough to touch the trunk, but when a student named Davey Gudgeon almost lost an eye they were forbidden to go near it.
Lupin's best friends, James Potter, Sirius Black and Peter Pettigrew, figured out in time the reason behind their friend's monthly disappearances, and became animagi illegally to keep Lupin company at the full moon. During this time, they discovered many of the secrets of the Hogwarts grounds, and preserved this knowledge in the Marauder's Map.
Severus Snape, a student loathed by Potter and his friends, also had an interested in Lupin's disappearances, though with malicious intent. Black thought it would be amusing to tell Snape where the tunnel lay, and how to get past the Whomping Willow, by pressing a knot which temporarily paralysed the tree. James Potter saved Snape from an encounter with Lupin in his werewolf form, though not in time to prevent Snape from seeing him as a wolf.
The tree remained at the Hogwarts grounds long after Lupin left the school. In Harry Potter and the Chamber of Secrets, Harry (the son of James Potter) and his best friend Ron Weasley flew an enchanted Ford Anglia to Hogwarts, because the barrier at Platform 9¾ wouldn't let them through, crashing into the Whomping Willow, damaging it.
In Harry Potter and the Prisoner of Azkaban, Harry fell off his Nimbus 2000 which flew into the Whomping Willow and was smashed to bits. After a series of complicated events, Harry Potter found himself and his friends in the Shrieking Shack along with Sirius Black, and Professors Lupin and Snape, where he and his friends learned the story how the tree came to be planted, and the true story of what happened to Potter's parents 12 years before.