In J.R.R. Tolkien's novels
Before the events of The Hobbit
Gandalf was originally created by Eru Ilúvatar (who created the universe) as a Maiar named Olórin, and was accounted as the wisest of them. He served under 4 of the Valar; their names were Manwë, Varda, Irmo, and Nienna. When the Valar decided that the Istari (an order of 5 Maiar in the form of old men) were to be sent to Middle-earth to help the free peoples defeat Sauron, Olórin was included as one of the five Istari.
However, at first, Olórin was too afraid of the mission because he was too weak to fight Sauron, but Manwë convinced him to overcome that fear, which was one of the reasons why Olórin had to go. Manwë insisted that he become the 3rd Wizard, but Varda convinced him to make Olórin the second. Olórin agreed with this, departed to Middle-earth with the 5 other Istari (which they will be known as Wizards in Middle-earth), and went under the name, "Gandalf".
In Middle-earth, Gandalf spent much of his time with the Elves, and learned a lot from them. He also continued to gather information about Sauron, and eventually suspected that the Necromancer in Dol Guldur is, indeed, Sauron returned. He entered the fortress, but Sauron retreated for a short period of time, and the evil of Mirkwood was greatly reduced. However, Sauron returned to Dol Guldur in the year TA 2850, and has rebuilt his forces. In the same year, Gandalf found Thráin II, father of Thorin Oakenshield, who was dying after having been seriously tortured. Before he died, Thráin gave Gandalf a map and a key to the Lonely Mountain. Eventually, Gandalf would discover that his worst suspicions (that the Necromancer is Sauron) are true.
In TA 2941, Gandalf already knew that the Necromancer of Dol Guldur is, in fact, Sauron returned, and that when Sauron was strong enough, he would attack Rivendell. Unfortunately, the only other power to resist Sauron's forces from Mirkwood to reclaim the kingdom of Angmar was the Dwarves of the Iron Hills. Worse, Gandalf knew that the evil dragon Smaug was still in the Lonely Mountain, and that Sauron would attempt to get him to his side and use Smaug to conquer Middle-earth, and Gandalf knew that Smaug had to be killed before the events of the War of the Ring. However, Saruman seemed reluctant to fight Sauron's armies, though none of the White Council knew that he already became a traitor.
One night, Gandalf visited the town of Bree, and found Thorin Oakenshield, son of Thráin, son of Thrór, who was also trying to defeat Smaug. It is revealed that Thorin is trying to reclaim the Lonely Mountain from Smaug, and afterwards, Gandalf and Thorin made a plan to draw Smaug out of the Mountain to reclaim it.
Gandalf visited the Shire and met Bilbo Baggins, and tried to convince him to join Thorin's company on the quest to reclaim Erebor, but Bilbo still didn't want to go. He went into his home, and Gandalf sketched a mark on his door. Soon, the company of Thorin Oakenshield arrives, and Thorin himself tells the story of what happened when Smaug took over Erebor. Gandalf then hands over the map of the Lonely Mountain and the key to Thorin Oakenshield.
The next day, Gandalf shows Bilbo a sheet that the Dwarves wrote, which shows that they hope Bilbo will join the company. Bilbo tries to ask a question, but Gandalf tells him that there's no time, and that he has to go now. After Bilbo caught up with the 13 Dwarves, Gandalf rode on a white horse, who brought a lot of handkerchiefs, and also Bilbo's pipe and tobacco as well. Some time after this, Gandalf mysteriously left, but came back the next morning to turn the hill trolls Tom, Bert, & William (who were already cooking the Dwarves) into stone with sunlight, as hill trolls turn to stone once the sunlight hits them. In the trolls' cave, Gandalf finds the sword Glamdring.
Eventually, the Company reaches Rivendell. It is here where Lord Elrond revealed the moon runes on Thorin's map, which were vital to their quest to reclaim Erebor; they said, "Stand by the grey stone when the thrush knocks, and the setting sun with the last light of Durin's Day will shine upon the keyhole."
Thorin's company then reach the Misty Mountains, and see two stone-giants clashing with each other; Gandalf is able to witness this as well. During the thunder-battle, they are able to escape, and hide in a cave. However, a crack opened at the back of the cave, and Bilbo yelled. Unfortunately, they fell in, and were captured by goblins. By this point, the company doesn't even know where Gandalf is.
In Sir Peter Jackson's films
Before The Hobbit trilogy
One year before the events of The Hobbit trilogy, Gandalf the Grey meets with a dwarvish prince called Thorin Oakenshield at The Prancing Pony in Bree. Gandalf offers his help to Thorin in reclaiming Erebor, the Lonely Mountain, the once powerful Dwarf stronghold of the East that had been taken by Smaug, a powerful and evil dragon. Gandalf also reveals that he was a longtime friend of Thorin's father, Thráin, who went missing after a climatic battle with orcs in Moria. Before the battle, Gandalf urged Thrain to reclaim Erebor by uniting the sevens armies of the original Dwarf-lords and with Thrain missing, Thorin is heir to the throne and he urges him to do so. Thorin agrees but in order to do so, he needs proof of loyalty which lies in a jewel called the Arkenstone. Whomever possess the Arkenstone reigns over the dwarf kingdoms of Middle-Earth but the jewel lies somewhere in the massive treasure hoard now guarded by Smaug. Gandalf suggests that they are going to need a burglar to help take it. Gandalf also urges Thorin about the quest due to the fact that he is now being hunted and has spread the word of payment for his head.
The Hobbit trilogy
The Hobbit: An Unexpected Journey
Sixty years before the events of The Lord of the Rings: The Fellowship of the Ring, Gandalf is shown to be a guest of the Shire for many years, back when Bilbo Baggins himself was a boy. One day, while enjoying an afternoon smoke outside his Hobbit hole, a young Bilbo Baggins finds Gandalf standing in front of him, unaware of who he is. Gandalf offers Bilbo the adventure of a lifetime which he kindly declines. Gandalf then sketches a glowing mark on the door and takes off. Later that night, Bilbo finds his house full of dwarves accompanied by Gandalf who eat and drink every food in his home. When Thorin himself arrives, he and Gandalf explain their quest to Bilbo and the other dwarves. Gandalf hands Thorin a map of Erebor and a key to a hidden door given to him by Thrain.
The Lord of the Rings trilogy
The Lord of the Rings: The Fellowship of the Ring
Three thousand years after the battle of the Last Alliance and 60 years after the events of The Hobbit, Gandalf travels to the Shire, home of the hobbits, to attend the birthday party of his old friend and fellow adventurer, Bilbo Baggins, who is turning 111. Loved by many in the Shire, he is a good friend, loved by the children for his fireworks, and that being said, considered a disturber of the peace by the elder folk. He runs into Bilbo's nephew, Frodo who is close to Gandalf more than anyone in the world. Gandalf arrives at Bilbo's and discovers that Bilbo looks fifty years younger. Unbeknownst to Gandalf, that Bilbo found the One Ring of Sauron many years ago and stole it from its predecessor: Gollum. Those who possess the ring will not die; of old age that is.
During the party, Gandalf lights off many fireworks and behind his back, two troublemaking hobbits, Merry and Pippin light one of his biggest fireworks and almost started a wildfire. Gandalf catches them and puts them to work for punishment. Bilbo gives a birthday speech and as a prank to get away from everyone, he puts on the ring and disappears back to his house. Gandalf realizes what Bilbo had done and confronts him; telling him that that ring should not be in the hands of anyone and must put it away in secret and safe from harm. The lust of the ring which makes all who wield it greedy, want for it and "precious" takes hold of Bilbo and tries to attack Gandalf, but he scares him with his magic and brings him back to normal. Bilbo apologizes and decides to journey to Rivendell where he can finish his book and live his last few years in peace. He entrusts the ring to Gandalf and asks him to leave it in Frodo's hands where it will be safe.
Gandalf does so and Frodo tucks it away where none could find it. Gandalf fears that since Bilbo put the ring on, Sauron now knows where it is and will send his forces of evil to retrieve it. He leaves to find answers and learn more about the history of the ring. He travels to Minas Tirith, the capital of Gondor, into the deep rooms of the archives. He reads about the last battle between Sauron and men and elves and how Isildur, the last true king of Gondor severed Sauron hand, the one the ring was on and destroyed Sauron's body. Sauron poured a bit of his blood in the ring so his spirit still lives and will continue to live as long as the ring is alive. He gaves great haste to return to the Shire and find the ring. He has Frodo throw it in the fireplace and being exposed to fire will reveal writing on it. Gandalf tells Frodo about the ring to which Frodo decides to hide it again. Gandalf says it will not work because Sauron knows it is in the Shire.
He then has Frodo embark on his own journey to bring the ring to Rivendell where the elves can keep it safe. He tells him that he must ride to Isengard, to seek counsel with Saruman the White and afterwards he will rendevous with him at the village of Bree, just outside the Shire, at the inn of the Prancing Pony. He overhears someone outside and jumps him. It was Samwise Gamgee, Bilbo's gardener and Frodo's shy but at times, stout hearted friend. Gandalf decides to have Sam accompany Frodo and his "protector", and he also threatens Sam to guard Frodo with his life and Sam intended to keep that promise to the grave. Before departing, he tells Frodo to keep off the main roads because Sauron's scouts will be traveling on them and to never put the ring on as it will lure the scouts to him.
Gandalf arrives to Isengard and tells Saruman everything. Saruman was disappointed that Gandalf did not sense the ring all this time and that time is running out. He explains that even though Sauron's body was destroyed, he used his black magic to take the form of a giant eye: lidless, wreathed in flame and perched on the top of his tower where he can see all. Saruman goes on to say that Sauron is gathering all evil: orcs, trolls and wicked men to make a enormous army and launch an assault upon the whole world. Gandalf asks how Saruman could know all this and he tells him that he has seen it through an ancient crystal-ball-like stone called the Palantir. Gandalf says that the stone was not a good idea and senses Sauron around it. Saruman tells Gandalf that the scouts Sauron sent were nine Ringwraiths: very powerful, immortal, wicked men who can sense the ring like Sauron can. He guarantees that the wraiths will find Frodo and kill him to which Gandalf rushes out to rescue him but Saruman closes the doors, trapping them both. Saruman reveals that he has sworn loyalty to Sauron and asks for Gandalf to join as well. Gandalf refuses and the two wizards duel. Saruman gains the upper hand and takes Gandalf's staff and uses both staffs to levitate him and hurl him up to the top of the tower and imprison him there.
During his imprisonment, orcs arrive and destroy all the trees surrounding Isengard and turning it into mines and making weapons and breeding new orcs for the war. A moth appears before Gandalf and he telepathically sends the moth on a mission. Many days later, Saruman went up to the tower to attack Gandalf and gives one last chance to surrender and join with him. The moth and a giant figure appears behind Saruman. Gandalf assures Saruman that evil will fail and Sauron will be destroyed and jumps off the tower and onto the figure which was Gwahir, king of the Eagles and Gandalf returned to Rivendell and was at Frodo's bedside when the hobbit was stabbed by a wraith and almost became one but was brought back and healed.
Gandalf spoke with Elrond, ruler of Rivendell and fellow member of the high Elf council. Gandalf informs Elrond of Saruman's betrayal and his involvement in Sauron's war. Saruman is breeding a larger and more powerful breed of orc called the Uruk-Hai which are impervious to sunlight and can run very fast and seldom be exhausted. Elrond tells him that Gondor, Rivendell and all their allies do not have the strength to fight both Sauron and Saruman and that the ring cannot stay in Rivendell. Elrond summons a meeting of all rulers of men, elves and dwarves to discuss the future of the ring and tells Gandalf that soon the elves will depart to the grey havens and will no longer be of service anymore. Gandalf tells that this war will be the fought on the battlefield of men and therefore all hope must be put in man. Elrond disagrees and has disliked men for many years. He explains that he was at the great battle and witnessed Isildur destroying Sauron. Elrond told Isildur to destroy the ring but fell under its greedy power and kept it alive, leading Elrond to no longer trust men. Gandalf says there is one man that Sauron fears and could win the war but Elrond tells him that that man does not wish to rule and has chosen exile. The man is revealed to be Strider, a ranger of the north who guided Frodo and the hobbits to Rivendell and is in love with Elrond's daughter, Arwen.
During the council, one of the men, Boromir, son of the Steward of Gondor sees the ring and wants to take it but Gandalf stops him and starts chanting the words written on the ring against his own will. Boromir wants the ring to be used against Sauron but Strider tells him that that is what Sauron wants them to do. Boromir rebuffs him and the council learn that Strider is Aragorn, the heir of Isildur and destined to be the next king of Gondor. Elrond and Gandalf explain that the ring must be taken back to Mordor and thrown into the volcano where its lava is strong enough to destroy it. Everyone in the council refuse to volunteer and believe that it is a suicide mission and they begin to bicker at each other about their prejudices and Frodo decides that he will take it. Gandalf, Aragorn, Boromir, an elf called Legolas, a dwarf called Gimli, Sam, Merry and Pippin all agree to guide Frodo to Mordor and they became the Fellowship of the Ring.
During the journey, Saruman sends flocks of crows to spy on the fellowship and they keep hiding from the birds. Gandalf decides to take the pass of Caradhras, over the Misty Mountains, during a blizzard. Saruman learns of this and tries to start an avalanche and wipe them out. Gandalf overhears Saruman chanting in the distance and tries to counterblock the spell. Saruman's spell was too powerful and caused an avalanche. The fellowship survives but they could go no further and had to go back. Boromir suggested they take the west road to Gondor but that road was too close to Isengard. Gimli suggested that they go through Moria. A giant mine beneath the mountain, inhabited by the dwarves. Gandalf feared those mines as an ancient evil slumbers there. He lets Frodo decide and he chose Moria.
They arrive outside the mine and try to open the secret door. Gandalf tried every password he could think of but to no avail. Frodo figures out the riddle to the door and the door opens. The are attacked by a kraken-like creature in the water outside and they escape into the mine. The kraken destroys the door leaving the fellowship no choice to move forward which was a four day journey to the other side. They find three passageways and Gandalf is unsure which way to go. While waiting, Frodo spots something and Gandalf tells him that it is Gollum, trying to get the ring back. Frodo tells Gandalf he wished that none of this had happened but Gandalf explains that it was meant to happen and that Frodo must make his own choices and do what he thinks he should do. Gandalf chooses the right passageway by following his nose and they end up in the great halls of Moria. They find a crypt and a tomb inside. Gimli learns that the tomb belongs to his cousin, Balin.
Gandalf feared this would happen as the dwarves were ambushed and killed by orcs. The orcs discover their presence and the fellowship battle with them. They were victorious but thousands more were coming and they needed to get to the bridge of Kazah-Dum which would lead them the way out. The orcs catch up to them and surround them but they hear a loud bellowing sound in the distance and the orcs run away. The fellowship ask Gandalf what it was and he says it was a Balrog. The ancient evil he feared had awakened and is a powerful and terrifying foe. They run to the bridge but the Balrog catches up to them and chase them to the bridge. Everyone except Gandalf make it across but Gandalf stays and tries to duel with the Balrog. He gives a powerful speech about how strong his magic is and says his famous quote "YOU SHALL NOT PASS!!!". Gandalf destroyed the bridge and the Balrog fell into the deep abyss below. The balrog used his whip to grab Gandalf and take him with him. Gandalf holds on to the edge, tells the fellowship to leave and get out and willingly lets go.
The hobbits became heartbroken as they lost their oldest friend. They traveled to a nearby forest inhabited by elves and they tell the Lady Galadriel of Gandalf's fate. Elves gave a lament for Gandalf.
The Lord of the Rings: The Two Towers
In the next film, the fellowship had scattered. They were ambushed by Saruman's Uruk-hai and engaged in battle. Merry and Pippin were captured and were to be taken back to Isengard. Boromir tried to save them but was slain. Aragorn, Legolas and Gimli laid his body to rest and ran off to rescue Merry and Pippin. Frodo and Sam journeyed to Mordor alone. The trio learn that the uruks were killed by men of Rohan and the two hobbits ran into Fangorn Forest. The trio go into the forest and Legolas senses a wizard nearby, whom they believed was Saruman. They began to strike but the wizard deflected their weapons and appeared before them in a bright light and his face covered. He tells them that Merry and Pippin are in safe hands and Aragorn demanded that he reveals himself. The wizard was none other than... Gandalf! He survived the fall and engaged in several battles with the balrog throughout the mine. He had one last duel with the demon on top of the mountain and slayed him. Gandalf was victorious but he became weary and was beginning to die. he was given another chance in life and was reincarnated into Gandalf the White. His magic gave him the power to be equally strong as Saruman or perhaps stronger than him and to take his place in the high order.
Gandalf and the others learn that Rohan's king, Theoden is under Saruman's mind control and allowing orcs to roam freely across the land and killing its citizens. The trio were given horses by the men who slew the uruks for them and Gandalf calls upon his own horse. An all white horse appears called Shadowfax, the lord of all horses. The night before they arrived to Theoden's palace, Aragorn speaks with Gandalf. Gandalf explains that if Aragorn follows his destiny to become king, it can change the outcome of the war. He also tells him to not regret leaving Frodo to travel alone but Aragorn says that Sam went with him as he still continues to fulfill his promise to Gandalf. (At this time, Frodo and Sam have no idea that Gandalf is alive).
They arrive at Edoras, the palace of Rohan and are not given a warm welcome. They are asked to give up their weapons at the main door. The captain of the guard asks Gandalf to give up his staff but he says he needs it as it is his walking stick. Theoden's messenger, Grima Wormtongue, works for Saruman and has the guards attack Gandalf. The trio protect him and fight off the guards and Gimli knocks down Wormtongue to keep him from interfering. Gandalf tries a meditation process to release Theoden but fails. Saruman still believes that Gandalf is still the grey and taunts him. Gandalf throws off his grey cloak to reveal his white one and the sight of him blinds Theoden and Gandalf approaches closer and tries to perform his own exorcism. He says that he is now Gandalf the white and is more powerful now than ever. Through Theoden, Saruman says that Theoden will die and Rohan now belongs to him but Gandalf does not listen and continues breaking the spell. Saruman tries one more time to attack Gandalf but he gives the final blow with his staff and releases Theoden permanently. The spell being broken literally injures Saruman as if he got hit with the staff himself. Theoden returns to normal giving his strength and power back. He banishes Wormtongue and attempts to strike him but is stopped by Aragorn and Wormtongue escapes.
Theoden learns that while he was under Saruman's control, orcs have destroyed several villages under his rule, he banished his own nephew and three-quarters of his army and his own son was killed in battle. Gandalf and the trio participate in the funeral and Gandalf stays with Theoden at his son's grave. Theoden greatly weeps over his loss and feels guilty but Gandalf tells him that it was not his fault and that the spirit of his ancestors will welcome him in the great beyond. He gives a small prayer in the Rohirrim ancient language and prays for hope. He sees a boy and his sister in the distance who arrive to tell Theoden of the villages being destroyed. Gandalf tells him this what Saruman is planning to do and will do more unless he is stopped. Theoden decides to bring all survivors to Helm's Deep, the great fortress of Rohan. Gandalf believed it was a bad idea as if they are attacked, then there is no other way out. He then decides to go out and find Eomer, the nephew and bring his armies back to fight. Gandalf forsees this and tells Aragorn that he will return in five days with the army. He tells Aragorn that Theoden will need his help to protect Rohan and he must do everything he can to do so and he promises that he will.
In those five days, in Gandalf's absence, Theoden, his remaining army and several survivors travel to Helm's Deep and Saruman launches an assault on the fortress with an army of 10,000 Uruk-Hai. Elrond learns of the attack and dispatches hundreds of elves to help protect Rohan. Aragorn and Legolas lead the elves into battle while Theoden watches with his own army to be the backup. The Uruks breach the defense wall and then the fortress itself leaving Theoden, Aragorn, Legolas, Gimli and a few soldiers cornered. The next morning, the survivors are blocking the uruks from getting in and Theoden begins to lose hope. Aragorn remembers what Gandalf told him and that he will be arriving soon. Theoden then leads one last stand and leads a cavalry charge to get outside. They trample hundreds in their path and see Gandalf atop a hill in the distance. The army appears behind him and he leads the charge. The uruks position themselves to stop the charge but Gandalf uses his magic to increase the light of the sunrise and blind the uruks. They are forced to put down their weapons and are easily trampled. The remains of the army run away and hide in a nearby forest where they are squashed by the trees for revenge for cutting many down. After the battle, Gandalf, Theoden, Aragorn, Legolas, Gimli and Eomer ride off to Isengard to confront Saruman.
The Lord of the Rings: The Return of the King
The film opens with Gandalf and the others journeying to Isengard to confront Saruman. They find Isengard completely flooded. Merry and Pippin had the ents and trees of Fangorn forest rise up and take Isengard by force. They do so, killing every orc, and flooding the land and destroying all the mines. Saruman and Wormtongue are left trapped in the tower. The fellowship approach the tower and Saruman is seen on top of the tower. He sarcastically asks Theoden for peace but Theoden quickly refuses it. He angrily asks what Gandalf wants and he asks for information. Saruman shows the group his palantir and that Sauron is planning an assault and will attack somewhere soon and that all will die. He then taunts the group saying Aragorn will never be king, Theoden has failed as a king and Gandalf was a fool sending Frodo on the quest and that he will fail and die.
Gandalf refused to listen and demands that Saruman reveals Sauron's plan. Saruman grew angry and hurls a fireball at Gandalf which cloaks him in fire. It had no effect on him and he breaks Saruman's staff. Gandalf demands again and Saruman agrees but only if the ents and Rohan back off and stop treating him like a prisoner. Wormtongue then stabs Saruman for being treated poorly and he is shot with an arrow by Legolas. Saruman then falls off the tower to his death. Pippin finds the palantir and Gandalf sternly asks him to give it to him.
They return to Edoras and have a victory banquet. Later that night, Pippin becomes obessesed with the palantir and looks at it and sees Sauron's plan. Sauron tries to brainwash Pippin but Gandalf saves him and brings him back to normal and demands to know what happened. Pippin said he saw a dead tree and a city burning and Gandalf realizes that its Minas Tirith. Sauron plans to attack Gondor and if he succeeds, then no one can stop him from taking over the world. Aragorn volunteers to go and warn them but Gandalf tells him that he has a mission for Aragorn that he must fulfill in order to win the battle. Gandalf travels to Minas Tirith with Pippin and arrive three days later. Pippin sees the tree outside the citadel and Gandalf tells him that Gondor only has a steward, Lord Denethor. Before going in, Gandalf tells Pippin that Denethor is Boromir's father and he may not know about his son's death, so do not mention it.
He also tells him not to mention Frodo or the ring or Aragorn. They appear before Denethor who is holding Bormoir's broken horn and mourning his loss. Pippin has a flashback of Boromirs death and offers to serve Denethor in debt of Boromir saving him and Merry. This angered Gandalf and he warns Denethor about the was and Sauron's plan to siege Gondor. He asks to have the beacons lit and ask for help from Rohan. Consumed by grief, he refuses to listen and believes that is all a plot to overthrow him. He tells Gandalf that he knows all about Aragorn and does not want him to be king. Gandalf abruptly storms off and begins to lose hope for Gondor. Pippin sees Mordor for the first time and Gandalf senses that the battle will begin soon. Pippin wants to leave but with Denethor overcome by grief, Gandalf prepares to lead Gondor in battle if he must.
That night, Pippin prepares to serve his new master and begins to worry about Frodo and Sam. Gandalf tells him that Frodo will succeed and stares at Mordor. He tells Pippin that Sauron will unleash not only orcs, but easterlings, haradrim and corsairs upon Gondor and if the city of Osgiliath is taken, then the city will have no more defenses. He also learns that Sauron has appointed the nine ringwraiths as his generals of the orc army and will lead them in war. The ringwraiths' lair, Minas Morgul shoots out a bright white light into the air which signals that the armies of Mordor are ready to march. They sneak into Osgiliath via the river and Faramir, Boromir's brother tries to keep them out but fails. Meanwhile, Gandalf has Pippin light the beacon and Rohan gets the message and call upon every Rohirrim to ride to war. Faramir and his army are overrun and retreat to Minas Tirith where they are chased by three ringwraiths on their flying beasts. Gandalf and Pippin ride out and with his staff, he shines a very bright light and blinds the beasts, having them retreat. Faramir meets Pippin and tells Gandalf that he ran into Frodo and Sam and that they're traveling the dangerous way. Denethor becomes furious with Faramir failing to take the ring from Frodo and losing Osgiliath. He then orders him to retake the city which is heavily overrun and he sadly agrees. Gandalf begs Faramir to not go but he does and there were no survivors.
When the entire orc army arrived on Pelennor Fields, Denethor lost all hope and ordered his men to abandon their posts and flee. Gandalf appears behind him and beats him with his staff and knocks him unconscious. Gandalf leads the men on the wall and they strike down many trolls and orc towers. The archers strike down hundreds of orcs, trying to breach the gate. Pippin appears to fight and Gandalf tries to protect him. When an orc appears behind Gandalf, Pippin steps in and kills the orc. Later that night, the orcs succeed in breaching the gate and Gandalf and the Gondorians try to hold them back. Pippin finds out that Faramir survived and Denethor plans to burn them both alive and he runs off to find Gandalf.
Gandalf and the Gondorians are holding on and they are forced to fall back to the second level. Pippin finds Gandalf and tells him what Denethor was doing and the two quickly rides to rescue Faramir. They are intercepted by the Witch King, the leader of the ringwraiths and Gandalf tries to ward him off. The king uses his own dark magic, breaks Gandalf's staff and the fell beast tries to eat him but the king hears horns in the distance and flies off. Rohan had finally arrived and lead a tremendous charge, trampling thousands of orcs. Gandalf and Pippin arrive at the tombs when Denethor was about to light the pyre. Gandalf grabs a soldier's pike and throws Denethor off the pyre while Pippin jumps on to save Faramir. Denethor became angry and tries to kill Pippin but Gandalf saves him and has Shadowfax hit Denethor and throw him in the fire. Denethor runs off covered in flames and falls seven levels to his death.
Though Rohan was turning the tide of the battle, the Haradrim appeared with their giant elephants and began stomping on the Rohirrim. About three, four levels up, orcs and a troll try to break open another gate, which Gandalf, Pippin and the surviving Gondorians try to hold. Pippin was afraid that this was the end but Gandalf cheers him up by telling him a poem of the Grey Havens. They fight bravely but Aragorn, Legolas, and Gimli arrive on the field with an army of ghosts, having fulfilled the mission Gandalf gave them. The dead wipe out every orc and elephant on the field and then in the city. Gandalf and Pippin witness Aragorn thanking the dead for the service and leaving in peace. During the aftermath, Gandalf tries to use his foresight to find Frodo but no luck and fears that he is in Mordor. Aragorn thinks of a plan to lure out the remaining orcs out of Mordor and give Frodo easy passage to Mount Doom. He and Gandalf and the rest of the fellowship lead what is left of the Gondor and Rohan army to the black gate for one final battle. The fellowship meet with the mouth of Sauron who tries to negotiate. Gandalf orders him and his army to leave Mordor and never return. The lieutenant shows them Frodo's mithril vest, making him believe he is dead. The lieutenant continues to taunt them about Frodo and Gandalf began to weep. The taunting greatly frustrated Aragorn and he decapitated the lieutenant saying he does not believe what he said.
The orcs march out of the gate and surround the fellowship. Gandalf holds onto the mithril and remind Aragorn of Frodo. Aragorn leads the charge and the army begin the battle. The fellowship slay down many orcs and the eight remaining ringwraiths appear to fight. One charges at Gandalf, but his old friend the moth appears before him and then the fell beast is attacked by an eagle. Gwahir and several other eagles appeared and fought the ringwraiths in the air and scared them away. Frodo succeeded in destroying the ring and the fellowship stop fighting and witness Sauron suffering. The eye of Sauron exploded and the impact destroyed the tower and caused an earthquake killing all the surviving orcs and Mount Doom erupted. Their cheering was brief as they believe that Frodo was dead. Frodo and Sam jumped onto a rock and waited to be rescued. Gandalf, Gwahir and two other eagles fly towards Mount Doom and rescue the two hobbits. Frodo wakes up days later in Minas Tirith with Gandalf at his bedside once again. Frodo could not believe that Gandalf was alive and the rest of the fellowship came in and became glad to see him. Soon after, Aragorn's ceremony to become king came and Gandalf, himself, crowned him. Aragorn was also reunited with Arwen and the four hobbits were thanked personally by Aragorn and they returned to the Shire.
Four years later, Sam was now married and with a family, Bilbo was near death and Frodo had finished writing his book about his adventure with the ring. Gandalf arrived to take Bilbo and the four hobbits to Rivendell. Gandalf, Elrond and Galadriel were about to leave Middle-Earth for the Grey Havens and asked Bilbo to join them. Gandalf said goodbye to his closest friends and wished them luck and his task was finally done. Frodo was asked to join them as well and he goes but embraces his "brothers" saying he'll miss them. Gandalf and Frodo then boarded the ship and sailed towards the great beyond into the Undying Lands.