Backstory:
Long ago, in the age of fairy tales, the world was filled with the power of light, and everyone was happy. But some wanted to hoard the power of light for themselves, and so darkness entered their hearts. A great battle was fought, and the world began to be consumed by darkness. However, the world was saved by the light in the hearts of children. They kept the light alive, and the world endured, though it was fragmented into many smaller worlds.
Kingdom Hearts 1:
Sora, Riku, and Kairi live on a small island world called Destiny Islands. Riku believes there must be more to discover beyond the confines of their island, so he recruits Sora and Kairi to help build a raft so they can leave their island. On the eve of their departure, Sora meets a sinister hooded figure deep within the heart of the island, standing beside a mysterious door.
That night, the mysterious door opens, and dark clouds and shadowy creatures flood over the island. Riku and Kairi are consumed by the creeping darkness, but before the darkness can swallow Sora, a key-shaped weapon appears in his hand. He fights off the shadowy creatures, but eventually his world is totally consumed.
At around the same time, Donald and Goofy learn that their King, Mickey, has left his castle to go fight the shadowy creatures and find out why worlds are disappearing into darkness. Mickey asks Donald and Goofy to seek out a key. They set out for Traverse Town, a world where people end up when their worlds are consumed, where they find a marooned Sora.
The three heroes learn that Sora's key-shaped weapon is called a Keyblade. They also learn that the shadowy creatures are the Heartless, born when darkness overtakes someone's heart. In addition, they learn that a man named Ansem has been studying the Heartless, but has gone missing.
Together, Sora, Donald, and Goofy set out to fight the Heartless, seal the magical Keyholes that protect the worlds from darkness, and find Mickey, Riku, and Kairi. Meanwhile, a clan of Disney villains, led by the fairy witch Maleficent, is trying to use the Heartless to claim the worlds for themselves and capture the Seven Princesses of Heart, beings with hearts of pure light. They've taken the adrift Riku under their wing, telling him that if he helps them, he can save Kairi, who has lost her heart.
Sora, Donald, and Goofy eventually catch up with Riku, but Riku believes Sora has abandoned Kairi for his new friends. This stokes dark feelings in Riku's heart, and drives him closer to Maleficent. Eventually, Riku is approached by the mysterious hooded figure from the island, who tells him to fully embrace the darkness—then possesses him.
As he storms Maleficent's castle, Sora finds the captured Seven Princesses—and learns that Kairi is one of them. Maleficent is trying to use the Seven Princesses to open a door to the source of ultimate power: Kingdom Hearts. He defeats Maleficent, then uses the Keyblade to sacrifice himself to restore Kairi's heart, which had come to rest within him due to the bond they share. He becomes a Heartless in the process, but Kairi restores him to his human form.
Sora challenges the possessed Riku, whose possessor is revealed to be Ansem. Ansem has covered enough worlds in darkness to reveal a doorway to Kingdom Hearts. Sora fights and defeats Ansem, then seals the door using the power of the Keyblade—with help from Riku and Mickey. The worlds lost in darkness are restored, but Sora, Donald, and Goofy end up stuck in a strange wilderness…
Chain of Memories:
As the three heroes wander the wilderness, a mysterious figure in a black cloak lures Sora, Donald, and Goofy to Castle Oblivion. They attempt to climb the castle, finding facsimiles of the worlds they've visited along the way, crafted from their memories. They also discover that they're starting to lose their memories as they travel deeper into the castle.
A group called the Organization, made up of other black-cloaked figures, has captured a girl named Namine who has power over memories that are connected to Sora. They are using her to brainwash Sora so they can use his Keyblade for an unknown purpose. As Sora climbs higher, his memories of Kairi are replaced with fake memories of Namine, and Sora's heart is overtaken by the desire to rescue Namine and keep her safe.
Sora defeats many members of the Organization as he climbs Castle Oblivion. One of the Organization members, Axel, has a change of heart and allows Namine to escape, and she tells Sora everything. Sora, still resolved to help Namine, goes on to defeat the Organization member in charge of Castle Oblivion: Marluxia. After he takes down Marluxia, Namine then offers to restore Sora's memory, but warns him that in the process he will need to enter a deep sleep and will forget everything that happened in Castle Oblivion.
As Sora makes his way through Castle Oblivion, Riku is also dragged into its lowest depths. The darkness infused in him by Ansem still nags at him, and Ansem begins to manifest again before his eyes. Haunted by Ansem, Riku faces his demons as he progresses through phantom worlds from his own memories.
Some members of the Organization create a "replica" of Riku, hoping it will serve as an obstacle to the real Riku and a pawn to help them manipulate and ensnare Sora. Riku fights off this cadre of Organization members and destroys the replica, then faces off against the remnants of Ansem, with help from Mickey and a mysterious man named DiZ. In the process, he begins to come to terms with the darkness in his heart, rather than fearing it or allowing it to overtake him.
After defeating Ansem, DiZ tells Riku about Sora's fate, and suggests that Riku and Mickey work in the shadows to keep the Organization at bay while Sora's memory is restored. He gives them black cloaks like the ones the Organization wears, and they set out with a new purpose.
358/2 Days:
A young boy named Roxas, who has no memory of his past, is inducted into the Organization by its leader, Xemnas. A few days later, he is joined by a girl named Xion, who also has no memories—and whom others in the Organization call a "puppet." Roxas has difficulty finding his way at first, but begins to come out of his shell after befriending Axel. Before long, though, Axel is sent off to Castle Oblivion.
In Axel's absence, Roxas befriends Xion and learns that, like him, she can wield the Keyblade. The Organization wants them to use their Keyblades to gather hearts. As Nobodies, the shells of people who have lost their hearts, they hope to use the gathered hearts to summon Kingdom Hearts and gain hearts of their own.
When Axel returns from Castle Oblivion, he reconnects with Roxas and forms a friendship with Xion. The three of them meet up every day at Twilight Town to have ice cream together, and they become best friends.
But a rift begins to form between them as Xion begins to search for the secrets of her origins. Axel is also secretly conspiring with another member of the Organization, Saix, to learn what Xemnas is up to, and why he is looking for a secret chamber in Castle Oblivion.
Xion learns from Riku that she is actually a replica created from Sora's lost memories of Kairi. She also learns that Roxas is Sora's Nobody, born when Sora sacrificed himself and became a Heartless to save Kairi. Both of them need to reunite with Sora so he can wake up—but as time passes, they are developing an independent sense of self, apart from Sora, further jeopardizing any chance of his recovery.
Xion is torn between staying with her friends and reuniting with Sora, but decides that bringing Sora back is the right thing to do. She confronts Roxas, forcing him to destroy her so she can return to Sora. Because she was created from Sora's real memories, disassembled by Namine, returning to Sora causes memories of Xion to vanish. Roxas is left only with a vague hint of memory and the pain of loss.
Driven by this loss, Roxas goes to confront Xemnas, but Riku gets to him first. Riku fights and defeats him, then brings him to DiZ and Namine so he can also be returned to Sora.
Kingdom Hearts 2:
Roxas is being held by DiZ in a virtual replica of Twilight Town, used to keep him safe from the Organization while he is guided to Sora. Axel attempts to get him back, but is thwarted. With encouragement from Namine, who tells him that he won't completely disappear after he returns to Sora, Roxas resigns himself to his fate, and Sora can finally reawaken.
Sora, Donald, and Goofy meet Master Yen Sid (sorcerer from The Sorcerer's Apprentice), the former teacher of Mickey. He tells them that a new threat has emerged alongside the Heartless: the Organization and their army of Nobodies. They set off to fight off the Nobodies and the Heartless across the worlds.
Eventually, they reunite with Mickey, who explains that the "Ansem" they defeated in Kingdom Hearts 1 was actually the Heartless born from a man named Xehanort. Xehanort was an apprentice to the REAL Ansem—Ansem the Wise. Xehanort took Ansem's name in order to continue research into the darkness that Ansem the Wise had abandoned.
Xemnas, the leader of the Organization, is actually the Nobody counterpart to this fake Ansem. The other members of the Organization are a mixture of Ansem the Wise's other apprentices (corrupted by Xehanort), and other individuals recruited by Xemnas. They want to use the hearts released by Sora's Keyblade as he fights the Heartless to summon Kingdom Hearts.
Axel continues to try to get Roxas back via Sora, capturing Kairi as bait, but Kairi falls into Xemnas's hands. Sora eventually tracks the Organization to their stronghold, and Axel repents for capturing Kairi by sacrificing himself to give Sora a chance at rescuing her.
Sora reunites with Kairi and Riku in Xemnas's castle, where Riku and Kairi are able to wield Keyblades of their own. DiZ reveals himself to be Ansem the Wise before sacrificing himself to disrupt Xemnas's progress toward summoning Kingdom Hearts. Together, Sora and Riku defeat the remaining lackeys of the Organization, then challenge and destroy Xemnas. After the battle, he and Riku finally return to Destiny Islands.
Birth by Sleep(10 years before the events of Kingdom Hearts 1):
Terra, Ventus, and Aqua are all students of the Keyblade, training under Keyblade Master Eraqus in the Land of Departure. Ventus has little memory of his past before meeting Terra and Aqua. As Terra and Aqua face their Mark of Mastery exam, another Keyblade Master named Xehanort (who is much older than Ansem's young apprentice) appears to witness their test. He goads Terra into being tempted by darkness during the trial, and so Terra fails his exam while Aqua earns the rank of Keyblade Master.
Eraqus sends Terra and Aqua out to face a threat known as the Unversed, creatures born from negative emotions with fledgling potential for evil. Eraqus asks Aqua to keep an eye on Terra. As his friends leave on their quest, Ventus is taunted by a masked boy called Vanitas, who claims that he'll never see Terra again. The three friends all leave the Land of Departure to face the Unversed.
Terra becomes frustrated that Aqua and Eraqus don't seem to trust him, and shuns his friends, turning to Master Xehanort for guidance. Xehanort plays off of Terra's insecurities to tempt him into embracing his dark impulses. He declares Terra a Keyblade Master, telling Terra that Eraqus only denied him the honor because Eraqus fears the darkness. He also tells Terra to seek balance between light and darkness, rather than shunning darkness entirely.
Ventus learns that he was once a pupil of Master Xehanort, who tried to use his student to forge a powerful Keyblade known as the χ-blade, which is the true key that can open Kingdom Hearts. This χ-blade was at the center of the ancient battle in the Kingdom Hearts backstory—the Keyblade War. As the legends go, when equal powers of light and darkness clash, the χ-blade will appear.
To pursue this goal, Xehanort split Ventus in two: his light half remained as Ventus, and his dark half became Vanitas. Xehanort hoped these two halves would be able to fight each other and create the χ-blade. But Ventus was broken in the process, and Xehanort brought Ventus to Eraqus, under the pretense that he was repenting for a terrible mistake.
Distressed by this revelation, Ventus returns to the Land of Departure and attempts to get the truth from Eraqus. Realizing that Xehanort is still trying to create the χ-blade, Eraqus moves to destroy Ventus. Terra appears just in time to save his friend, and fights Eraqus off. As Eraqus relents, Xehanort appears and strikes him down, before covering the Land of Departure in darkness. Aqua shows up to seal the Land of Departure away using a special technique known only to Keyblade Masters—transforming her homeworld into Castle Oblivion.
Ventus, Terra, and Aqua all meet at the Keyblade Graveyard, the site of the ancient Keyblade War. They know that Xehanort wants them to fight—so he can force Ventus and Vanitas to clash and so Terra can be tempted again by darkness—but they feel they have no choice. Terra attempts to subdue Xehanort, but Xehanort uses the growing darkness in Terra's heart as an anchor to possess him—taking on Terra's body as a younger, stronger vessel.
Ventus fights Vanitas to protect Aqua, and the two halves briefly reunite, χ-blade in hand. But Aqua manages to subdue Vanitas, and Ventus manages to kick Vanitas out of his heart. The χ-blade shatters… and Ventus falls into a deep sleep. His shattered heart finds refuge within the heart of a friend—the young Sora, still a boy on Destiny Islands.
Aqua hides the sleeping Ventus in a hidden chamber in Castle Oblivion to keep him safe until she can find a way to wake him up. She also attempts to rescue Terra from Xehanort's clutches, but is banished to the World of Darkness. Meanwhile, Terra-Xehanort appears to lose his memories in the inner struggle between Terra and Xehanort, and he is taken in as an apprentice by Ansem the Wise.
Dream Drop Distance:
After a Heartless and Nobody are destroyed, the original person they were born from reappears. As a result, the members of the Organization are beginning to reappear in their original forms—including Axel, who awakens as his original self: Lea. But some of their original selves have vanished…including Xehanort.
Sora and Riku are summoned by Yen Sid to undergo a Mark of Mastery exam. For their trial, they are sent to awaken the Sleeping Worlds—worlds that were lost to darkness but not restored after Kingdom Hearts 1. The Sleeping Worlds exist in dreams, outside time, and so Sora and Riku travel to a dream version of the night the Destiny Islands were swallowed by darkness as an anchor to access the Sleeping Worlds.
During their trial, Sora and Riku encounter a new enemy wearing the black coat of the Organization. They also encounter Xemnas and Xehanort's Heartless, mysteriously revived. In addition, they discover that many of the Sleeping Worlds they visit seem stuck in the past—they come across past versions of Mickey, Donald, Goofy, and others from their travels.
This is no coincidence. The new figure in the black coat is actually a younger version of Xehanort. He was contacted as a youth by Xehanort's Heartless, who found a way to travel backward in time after shedding his body and becoming a Heartless. As it turns out, Young Xehanort is actually also from Destiny Islands. He was told about his future, and recruited to gather various incarnations of Xehanort from across time.
After meeting up with Young Xehanort in the past, Xehanort's Heartless remained on Destiny Islands. It was his presence that caused the islands to be swallowed by darkness in Kingdom Hearts 1—and that allowed Young Xehanort to infiltrate Sora and Riku's quest to awaken the Sleeping Worlds.
Young Xehanort is gathering Xehanort's incarnations to reform the Organization, whose true goal is revealed: implant Xehanort's heart and mind into thirteen vessels of darkness. When originally forming the Organization, Xemnas hoped to use Nobodies, the empty shells of people who have lost their hearts, as potential vessels for Xehanort. But many of the Organization's members developed their own sense of self through their ties to other people, and developed hearts of their own without realizing it. Young Xehanort was the Organization's backup plan, designed to fill out the full roster of thirteen Xehanort vessels.
Thanks to Xehanort's influence, Sora's quest into the Sleeping Worlds has actually driven him deep into a nightmare. All the tormented individuals with whom Sora is connected—Roxas, Xion, and Ventus—have planted a deep-seated pain in his heart that the Organization is using to trap him in this nightmare. Their objective is to turn him into Xehanort's thirteenth vessel.
Riku awakens in the real world and heads off to confront the new Organization—led by Master Xehanort (now back in his much older incarnation). He arrives just in time to save Sora, and learns of Xehanort's master plan: to pit thirteen warriors of darkness against seven wielders of light to finally succeed at forging the χ-blade.
After escaping with Sora, Riku manages to awaken him from the nightmare. Because he was able to awaken the Sleeping Worlds and save Sora, Riku is given the title of Keyblade Master. Sora, having nearly succumbed to nightmares, does not earn this honor. Lea also manages to summon a Keyblade, and Yen Sid seems willing to train both Lea and Kairi to use the Keyblade.
Yen Sid believes that the time has come to attempt to rescue Terra, Ventus, and Aqua so they can gather Keyblade wielders to challenge Xehanort. That's where Kingdom Hearts 3 begins.