The story begins with Gandalf and the hobbit Peregrin Took (Pippin) delivering news to Denethor, the Lord and Steward of Gondor, that a devastating attack on his city by the armies of Mordor is imminent. Pippin then enters the service of the Steward as repayment of a debt he owes to Boromir, Denethor's dead son and preferred heir. (Boromir was a member of the Fellowship in The Fellowship of the Ring. He fell defending Pippin and his fellow hobbit Merry Brandybuck against raiding Uruk-hai.
Now clad in the uniform of the tower guard, Pippin watches the fortunes of war unfold, while the Lord Denethor descends into madness as the armies of Mordor press ever closer to Gondor's capital city of Minas Tirith. Faramir, Boromir's younger brother, returns from his campaign with the shattered remnants of his company and is soon ordered to ride out and continue the hopeless defence of Osgiliath against the Mordor orc horde. Osgiliath is soon overrun and a gravely wounded Faramir is carried back to Denethor. His people seemingly lost and his only remaining son all but dead, Denethor orders a funeral pyre built that is to claim both him and his dying son. Minas Tirith stands encircled and besieged.
Meanwhile, in far-off Rohan, Théoden and his Rohirrim are recovering from the Battle of the Hornburg, in which they defend Rohan against the forces of Saruman at great cost. Aragorn, having confronted Sauron through the palantír of Isengard, sets out to find the lost army of the undead oathbreakers who dwell in the Paths of the Dead, a mountain hall where they have been enslaved since their treachery ages ago. Helped by his companions Legolas and Gimli as well as a host of Rangers from Arnor in the north (the "Grey Company"), he sets out to recruit the Army of the Dead to his cause. As Aragorn departs on his seemingly impossible task, King Théoden musters the Rohirrim to come to the aid of besieged Gondor.
The forces of Mordor, led by the dreaded Witch-king of Angmar succeed in breaking through the gates of Minas Tirith, but are in turn crushed by the arriving cavalry of Rohan. The battle is also joined by a "black fleet with black sails". The forces of Mordor initially rejoice at its arrival, and then are horrified to see the banner of the King upon the ships. Aragorn has succeeded in using the Oathbreakers to capture the pirate fleet; and the men of Gondor once slaves on the ship are brought back to fight the horde of Mordor. In the following Battle of the Pelennor Fields the Witch-king is slain by Éowyn, the niece of King Théoden, and the invasion is broken at heavy cost — many warriors of Gondor and Rohan fell, and among them King Théoden. Denethor immolates himself and Faramir on his funeral pyre, but Gandalf and Pippin succeed in saving the gravely wounded Faramir, who is subsequently healed by Aragorn.
Knowing that it is only a matter of time before Sauron rebuilds his forces for another attack, Aragorn decides to draw out the hosts of Mordor with an assault on the Black Gate, believing that only then will Frodo Baggins and Samwise Gamgee (Sam) have a chance of reaching Mount Doom unseen by the Eye of Sauron. Because such an attack is suicide, he pushes forth with less than seven thousand troops. Book V ends as the army desperately attempts to hold the Mordor hosts at bay until Frodo can complete his task.
Book VI begins with Frodo held captive in the enemy stronghold of Cirith Ungol after being poisoned by Shelob. The brave and loyal Sam—who for a short time bears the One Ring in Frodo's place—rescues his master from torture and death by Orcs and the two navigate the barren wasteland of Mordor. They are overtaken by a company of Orcs and forced to disguise themselves in Orcish armour.
The two Hobbits, after a length of time, finally reach the Crack of Doom. Yet just as he is about to throw the ring into Mount Doom, Frodo succumbs to the Ring's power and refuses to let it go. Just then, Gollum, who had been following the pair still, attacks Frodo and bites off his finger with the Ring. Gollum gloats over his prize but loses his balance and falls into the lava below, taking the Ring with him. The Ring is destroyed, freeing Middle-earth from Sauron's power. Frodo and Sam are rescued by Great Eagles who carry them from Mount Doom. Upon Sauron's defeat, his armies at the Gate flee.
Aragorn is crowned King of Gondor outside the walls of Minas Tirith in a celebration during which all four hobbits are honoured for their contributions to the War of the Ring. A healed Faramir is given the title of Steward of Gondor and Aragorn marries Arwen, daughter of Elrond of Rivendell. After a series of goodbyes, the Hobbits return home, only to find the Shire under the control of "Sharkey", who turns out to be Saruman, diminished in power but not in malevolence. Merry and Pippin, now experienced warriors of Rohan and Gondor respectively, take the lead in setting things right again. They lead an uprising of Hobbits against Saruman, who is killed, thus freeing the Shire (see Battle of Bywater).
Over time, the Shire heals. The many trees that Saruman's Orcs cut down are replanted; buildings are rebuilt and peace is restored. Sam marries Rosie Cotton, with whom he had been entranced for some time, and Merry and Pippin lead Buckland and Tookburrow to greater achievements. Frodo, however, cannot escape the pain of his wounds, having been stabbed by the Witch-king and poisoned by Shelob. Eventually he departs for the Undying Lands in the West, with Gandalf, Bilbo Baggins, and many Elves. Sam, Merry and Pippin watch them depart and return home. Now heir to all of Frodo's possessions, Sam is greeted by his wife Rose and his daughter Elanor. In the last line of the book Sam says to Rose: "Well, I'm back."