- "The Mad King was obsessed with it. He loved to watch people burn, the way their skin blackened and blistered and melted off their bones. He burned lords he didn't like. He burned Hands who disobeyed him. He burned anyone who was against him. Before long, half the country was against him. Aerys saw traitors everywhere. So he had his pyromancers place caches of wildfire all over the city. Beneath the Sept of Baelor and the slums of Flea Bottom. Under houses, stables, taverns. Even beneath the Red Keep itself."
- ―Ser Jaime Lannister
As he neared the end of his life, and his mental state deteriorated, Aerys developed an obsession with wildfire. He enjoyed watching people burn, and in his paranoia would burn anyone he saw as an enemy. Eventually, he had Wisdom Rossart, head of the Alchemists' Guild and whom he later named Hand of the King, place caches of wildfire all over King's Landing.
After the death of Prince Rhaegar Targaryen in the Battle of the Trident, Lord Tywin Lannister marched to the capital with an army, promising to protect the city from the rebels led by Lord Robert Baratheon. Both the Master of Whisperers Varys and Ser Jaime Lannister of the Kingsguard urged Aerys to bar the gates and refuse Tywin entrance, but Grand Maester Pycelle urged him to let Tywin enter the city. Aerys did, and Tywin's army sacked the city.
Jaime once again urged Aerys to surrender, but the Mad King told him to bring him Tywin's head. He then ordered Rossart to burn the city to the ground. He believed that once the city was destroyed, he would rise from the ashes as a dragon. Before they could go through with this, Jaime killed Rossart and when Aerys tried to flee, Jaime stabbed him in the back and slit his throat to make sure he was dead. At this point, Lord Eddard Stark entered the throne room and discovered Jaime standing over the king he had just killed.
Hallyne, an acolyte at the time, had heard whispers of this plot. Though he did not know the details, he heard that Aerys had engaged the Guild's greatest Wisdoms for an ultimate weapon to use against Robert. He considered it unfortunate that King's Landing fell before it could be used and that many of the Wisdoms disappeared during the sack, whom he believed to be victims of ignorance and envy.
As part of the surge of visions and knowledge given to him by the Three-Eyed Raven prior to the latter's death, Bran Stark has a vision of members of the Alchemists' Guild preparing and amassing wildfire under orders of the Mad King. The wildfire barrels were eventually found by the little birds under Qyburn's orders, after the maester got intrigued by the rumoured plot after being told of it by Cersei Lannister.
Tyrion tells Daenerys about her father's plot to destroy King's Landing, in attempt to dissuade her from destroying her enemies' cities.
The wildfire stored under the Great Sept of Baelor is ignited during the Sept trial of Cersei and Loras Tyrell. Cersei had intentionally forgone the trial in order to watch the Great Sept explode and her enemies die within it. 
In the books
in the A Song of Ice and Fire novels, the wildfire plot occurs in a similar fashion, but is explored in greater detail.
After the rebel victory of Robert Baratheon at the Battle of the Bells, Aerys was fearful that Robert might actually win the war and enlisted the aid of several members of the Alchemists' Guild in a plot to destroy the city of King's Landing should the rebel forces prove victorious. Jaime was present when this was planned, while the rest of the Kingsguard was absent and went with Crown Prince Rhaegar Targaryen.
Thousands of jars of wildfire were gathered and placed in key positions under King's Landing. Hundreds were placed under the Dragonpit and the Great Sept of Baelor, under every one of the city's gates and even under the Red Keep itself. The pyromancers did their work in secret and did not trust their apprentices with the knowledge.
The rest of the royal family were unaware of this; apparently Queen Rhaella Targaryen's eyes had long been shut to her husband's insanity, and Rhaegar was busy marshaling an army. However, the Hand of the King, Lord Qarlton Chelsted, grew suspicious of the constant comings and goings of the pyromancers from the Red Keep. When Chelsted discovered the plot, he confronted Aerys; Jaime, who had always considered Chelsted a sycophant like his predecessors, notes in later years that he was impressed Chelsted found the courage within himself to confront the Mad King directly. When reasoning, threatening, jesting and even begging on his part failed to dissuade Aerys from this course of action, Chelsted resigned his handship, flinging his chain of office at Aerys's feet. Aerys had him burned alive for this (according to Jaime, he was dipped in wildfire and set alight) and made the pyromancer Rossart his new Hand of the King. Two weeks later, Tywin Lannister showed up at the gates with his army.
During the Sack of King's Landing, Aerys commanded Rossart to ignite the wildfire to destroy the city and Jaime to bring the head of his father, Lord Tywin Lannister. Jaime, however, intercepted and slew Rossart to prevent his message from reaching other pyromancers. Jaime then slew Aerys, slitting his throat. A few days later Jaime hunted down and killed Garigus and Belis, the other pyromancers involved in the plot.
The TV series never explicitly stated this, but Jaime's story to Brienne about the wildfire plot in "Kissed by Fire" explains how the Alchemists' Guild was able to provide such a massive amount of wildfire on short notice for the Battle of the Blackwater.
This had to be condensed somewhat for TV, as in "The Ghost of Harrenhal" Wisdom Hallyne simply says that they have been working tirelessly at Cersei's order and have made over 7,800 jars of wildfire. In the books, several major caches of wildfire left over from the Mad King's plot were inadvertently discovered hidden throughout the city, and were reacquired by the Alchemists. A particularly large cache was found under the Great Sept of Baelor, and the High Septon was so frightened of the dangerous substance that he begged the Alchemists to take it back so they could store it somewhere safe. Another large cache was discovered by accident when a group of whores entertaining their patrons in the ruins of the Dragonpit inadvertently fell through the rotted floor into a hidden chamber beneath containing jars of wildfire left over from the plot. Before they found these hidden caches, the stock of wildfire in the Guildhall of the Alchemists was only 200 jars. Cersei told Tyrion that they would have 10,000 jars of wildfire ready for the battle, but he thought it was only a boast until he saw the massive stockpile with his own eyes. The remaining Alchemists were still working tirelessly to finish the remaining 2,000+ jars of wildfire that were ordered, but Hallyne admitted they could never have met Cersei's massive order if they hadn't found the hidden stockpiles (in the TV version, Hallyne says that they currently have 7,800 and are working tirelessly to make more for the upcoming battle). A major reason they couldn't make 10,000 jars on short notice is because the Alchemists' Guild (which had already been in a long decline) never fully recovered from the loss of much of its senior leadership during the Sack of King's Landing - which, as Jaime reveals, occurred because after he killed the head pyromancer (Rossart), in the days after the city fell to the rebels he personally hunted down and killed all of the leading pyromancers who knew about the wildfire caches.
Wildfire only grows more potent with age, so the massive stockpile of wildfire over 15 years old produced extremely powerful flames.
Wildfire plot · Sack of King's Landing · Siege of Storm's End