The Sack of King's Landing was one of the closing events of Robert's Rebellion. It consisted of a devastating assault on the city of King's Landing and its inhabitants by the army of House Lannister. It effectively ended the civil war and the rule of House Targaryen.
After the Battle of the Trident, during which the Targaryen army had been defeated and Crown Prince Rhaegar slain, Robert Baratheon's victory was virtually assured. Lord Tywin Lannister, who had remained neutral during the conflict, ignoring pleas for aid from both sides, called his banners and marched on the capital. He pledged to defend the city against the approaching rebel host but once the gates were opened to them, Lord Tywin ordered his army to sack the city, as a show of loyalty to the new king.
As Lannister soldiers brutally rampaged through the city, the Mad King, Aerys II Targaryen was personally murdered by Ser Jaime Lannister, a member of his Kingsguard. Most of the king's loyal retainers and those family members who had not fled to Dragonstone also perished including, infamously, Rhaegar's wife Elia and their two children at the hands of Lannister bannerman Ser Gregor Clegane.
As Robert had been wounded in his battle with Rhaegar, his friend Ned Stark led the rebel forces to take King's Landing from Aerys, but they arrived after the attack had taken place and the royal family murdered. Horrified at such a treasonous way to win the war, Ned demanded that Tywin and Gregor be punished for their crimes. However, much to Ned's dismay, Robert refused to punish them, as his immense hatred for the Targaryens overcame his sense of justice, and to his shame, Robert knew that his claim to the Iron Throne would have be threatened by the children's survival.
Though the war by then was won, the Sack of King's Landing, as well as the deaths of Elia and her two children, earned House Lannister the hatred of almost all of Westeros, particularly House Martell.
Catelyn Stark talks to her son Robb about the Sack of King's Landing. She mentions that Tywin Lannister slew the young Targaryen children, referring to the murders of Prince Aegon and Princess Rhaenys, and how he has not gotten any more merciful with age.
Jaime reveals to Brienne that, when his father marched to the gates of King's Landing, both Jaime and Varys advised Aerys not to trust Lord Tywin. Aerys, however, followed Grand Maester Pycelle's counsel and had the city gates opened, allowing the Lannister forces to sack the city. Jaime advised Aerys to surrender, only for the Mad King to demand Jaime bring him his father's head and order Lord Rossart to ignite the caches of wildfire hidden throughout the city to burn it to the ground with all its inhabitants. Jaime killed Rossart and then Aerys, who kept muttering "burn them all" as he died. Aerys seemingly believed the massive wildfire inferno would also transmute him into a dragon. Afterward, Eddard Stark reached the throne room and found Jaime along with the two corpses.
During the Lannister-Sparrow conflict, Queen Cersei asks Qyburn to look into the rumor of Aerys's wildfire caches still remaining underneath the landmarks of King's Landing, most notably the Great Sept of Baelor. Through his little birds, he discovers this to be true, which he informs Cersei of after the announcement of her trial. On the day of her trial, instead of attending the Great Sept to face judgment, Cersei remains in the Red Keep, using the wildfire to destroy the Sept, eliminating the Faith Militant from their control of King's Landing, Margaery Tyrell's influence on Tommen Baratheon, Kevan Lannister and his control of the significant bulk of supporters previously held by Tywin Lannister, as well as many nobles and civilians in the process. In the aftermath, Tommen kills himself after witnessing the destruction, leading to Cersei's ascension as Queen of the Andals and the First Men.
The remaining wildfire caches that the Mad King sought to use during the Sack of King's Landing are ignited by Drogon during the Battle of King's Landing. King's Landing is sacked yet again, but this time the destruction and massacre are far more extensive.
In the books
In the A Song of Ice and Fire novels, the Sack of King's Landing is described as a particularly brutal and treacherous battle. During Robert's Rebellion, the forces of Houses Stark, Baratheon, Arryn and Tully had fought against those houses owing direct allegiance to House Targaryen, but of the other Great Houses only House Tyrell had sworn themselves to the Mad King's cause, and most of their forces were tied down in the south, besieging the Baratheon stronghold of Storm's End. House Martell of Dorne had reluctantly committed troops to the Targaryen cause only because the Mad King held Princess Elia Martell, the wife of Prince Rhaegar Targaryen, in King's Landing as a virtual hostage. The Martells were insulted by Rhaegar having started the war in the first place by taking Lyanna Stark as a lover, dishonoring his own marriage to Elia.
Upon hearing about Rhaegar's death, Aerys sent Queen Rhaella and Viserys to Dragonstone, but insisted on keeping Elia and her children by his side as hostages against possible Dornish betrayal; thus, Aerys was indirectly responsible for the deaths of Elia and her children.
During the fighting, House Greyjoy refused to take any part (though they did carry out some minor raids on loyalist holdings towards the end of the conflict as a token gesture to appease the rebels when Robert's victory became a forgone conclusion) and House Lannister similarly sat out the bulk of the war. Tywin felt insulted by many of the Mad King's own actions in the preceding years such as rejecting a proposed marriage between Tywin's daughter Cersei and Rhaegar and appointing Tywin's first son and heir Jaime to the Kingsguard - normally a great honor, but which insulted Tywin because it meant Jaime could not inherit Tywin's lands and title. However, after Robert's victory at the Battle of the Trident, Tywin, realizing the war was as good as won, rushed his army to King's Landing. In front of the city gates, he proclaimed that the Lannister force had come to defend the city from the rebel army rushing south under Eddard Stark. Following the advice of Grand Maester Pycelle, Aerys had the city gates opened to the "reinforcements", only for the Lannister troops to betray the city defenders and put them to the sword. The city was brutally sacked, with thousands of smallfolk caught up in the fighting and killed as well as soldiers on both sides. In later years, Tywin explained his reasons for acting with such haste; with the war nearly over, Tywin knew House Lannister's loyalty to the new regime would forever be in doubt unless he proved unequivocally he had forsaken all loyalty to the Targaryens. Tywin also relates that he feared a delay might bring him into conflict with Eddard, who was marching on King's Landing from the Trident, along with his fear that Jaime, still in the king's power, might do something stupid or Aerys might kill him out of sheer spite.
During the battle, Jaime killed Rossart and afterward killed the Mad King by slitting his throat (not by stabbing him in the back as told on the show) at the foot of the Iron Throne, thus foiling Aerys' plan to destroy the entire city by wildfire. Lord Roland Crakehall and Ser Elys Westerling, knights loyal to his father, burst into the throne room to see the end of the incident, ensuring that Jaime would forever be known as "the Kingslayer".
During the closing stages of the battle, Lannister troops scaled Maegor's Holdfast and killed Elia and her two children. According to rumor, the dread knight Gregor Clegane was the one who killed baby Aegon by bashing his head against a wall, then proceeded to rape and kill his mother, while Amory Lorch killed Rhaenys by stabbing her about fifty times.
In A Dance with Dragons, it is revealed that Varys switched baby Aegon with another baby; the Mountain killed the latter, while the former was sneaked away from King's Landing and handed to Illyrio Mopatis.
The death of the Mad King and his grandchildren saw the end of Robert's Rebellion and allowed Robert to claim the Iron Throne, even though the Mad King's son and daughter Viserys and Daenerys had escaped to the Free Cities to live in exile. Due to the death of Elia, Dorne and House Martell withdrew from active involvement in the affairs of the rest of Westeros afterwards. Jon Arryn was able to convince the Martells to swear fealty to Robert, but the fact Elia's killers were never punished was a serious bone of contention between Dorne and the Iron Throne.