Daenerys Targaryen transformed into the Mad Queen in the penultimate episode of Game of Thrones, but the reason she burned King's Landing to the ground is because she had already lost everyone and everything dearest to her. In "The Bells", the Dragon Queen singlehandedly won the Battle of King's Landing; riding Drogon, they annihilated Euron Greyjoy's Iron Fleet, wiped out the Golden Company, and eliminated the city's anti-dragon defenses. But after the bells rang signaling Cersei Lannister's surrender, Daenerys snapped and massacred the city with dragonfire.
Burning King's Landing to the ground is actually something Daenerys wanted to do at the start of the war with Cersei, but she was talked out of it by Tyrion. Indeed, at the beginning of Game of Thrones season 7, Daenerys was poised not just to conquer Westeros, but to do it easily and decisively. When she left Essos, Daenerys looked unbeatable. She had amassed an all-star contingent of loyalists: Tyrion was Hand of the Queen, she had the allegiance of Lady Olenna Tyrell of Highgarden, Ellaria Sand and the Sand Snakes of Dorne, Theon and Yara Greyjoy's Iron Fleet, as well as Varys and Missandei. In addition, Dany had her legions of Unsullied led by the eternally loyal Grey Worm, the Dothraki hordes, and her three dragons Rhaegal, Viserion, and Drogon. It was inconceivable that she could lose to Cersei - and yet, thanks to Tyrion's miscalculations, she lost consistently, except for the Loot Train Battle, which Dany won thanks to her dragons.
But by the time Daenerys reached Winterfell to fight the Great War against the Night King in Game of Thrones season 8, she had lost a significant amount of her military strength and her allies Ellaria Sand and Olenna Tyrell were dead. Also, her Iron Fleet was gone when Yara was captured by Euron Greyjoy. Worst of all, Viserion was killed and resurrected as the Night King's ice dragon. But beyond the icy reception she received, despite the presence of her lover Jon Snow, it was at Winterfell that Daenerys suffered her most personal loss: Jorah Mormont, her oldest and dearest friend, died defending her at the Battle of Winterfell. After the Great War was won, Daenerys lost even more loved ones when she was ambushed returning to Dragonstone; Rhaegal was killed by Euron and then Missandei was captured and beheaded in front of her by Cersei's orders.
In "The Bells", Daenerys had realized that her most loyal confidants and friends, whom she cultivated in Essos, were gone. Other than Grey Worm, who was mourning Missandei as much as Daenerys, everyone else who professed to support her were from Westeros. Worse, they were plotting behind her back because the secret that Jon Snow was actually Aegon Targaryen, the son of her dead brother Rhaegal, was spreading. Despite Daenerys' pleas, Jon told his family Arya and Sansa Stark the truth about his heritage, and Sansa wasted little time to inform Tyrion. Once Tyrion shared the info with Varys, the Spider was sending letters across Westeros trying to rally the highborn to support Jon's claim instead of Daenerys'. Because the inconvenient truth for Dany was that Jon's claim to the Iron Throne as the true heir was stronger than her own.
While many fans feel Daenerys' heel turn into the Mad Queen felt abrupt, forced, and unearned, her capacity for ruthlessness was established all throughout Game of Thrones. There's a legitimate argument that Daenerys' sudden transformation seems to betray seven seasons of character development as she resisted her Targaryen impulses and tried to rule with fairness and mercy. But from Daenerys' point of view, she was betrayed by her Westerosi advisers and the people from Essos she trusted most were taken from her while she was trying her best to save the realm. With Jorah and Missandei gone, the ones who are left, like Jon and Tyrion, are betraying her while still professing loyalty (for now).
One of Varys' claims in favor of Jon was that "people are drawn to him," but in Essos, Daenerys was just as magnetic and was loved wherever she went. All of her achievements are because of her unwavering belief in herself. However, Daenerys learned the hard way that no one really believed in her or loved her in Westeros; she was only met with mistrust. Tyrion even noted Sansa seemed "determined" not to trust Daenerys and, despite Dany's overtures of friendship, the Lady of Winterfell refused any truce. By the time Daenerys won at King's Landing, she realized that despite everything she'd lost, Westeros refused to trust her or love her (the final sign was Jon's hesitance to kiss her), so her only recourse was to make them fear her.
Daenerys is the only person in Game of Thrones actually qualified to rule because she spent years learning how to do it well. She really did come to Westeros to "break the wheel" and build a better world but the actions of her Westerosi allies showed they only wanted to preserve their broken system. In the end, it's Westeros that failed her and Daenerys lost everything in her quest for the Iron Throne. So she finally snapped and unleashed all of her frustrations, grief, and pure rage on King's Landing. It may be true that Daenerys was always the Mad Queen and finally showed her true colors, but it can also be argued that it's Westeros that made her Mad and she simply gave them what they wanted - and what they deserve.
Game of Thrones' series finale airs Sunday, May 19th @ 9pm on HBO.