SPOILERS AHOY!
The Origins of Sora and Xehanort
Three Keyblade apprentices, Terra, Aqua, and Ventus, train under Eraqus to become Keyblade Masters. However, one day, a Keyblade Master, Master Xehanort, mysteriously disappears, and at the same time, a vicious species known as the Unversed begin spreading throughout the worlds. Terra is sent to defeat the Unversed and locate Master Xehanort. Ventus is goaded into following Terra by Master Xehanort's enigmatic apprentice, Vanitas, and Aqua is instructed by Master Eraqus to bring Ventus back home and keep an eye on Terra. Along the way, Terra slowly begins to lose control in the darkness inside his heart, but finds solace with Master Xehanort, who appears to understand him.
Terra crosses paths with a young boy named Riku and chooses him as his successor. Ventus befriends fellow Keyblade apprentice, Mickey Mouse, and crosses paths several times with Vanitas and eventually learns that Vanitas is actually the embodiment of the darkness inside him, extracted by Master Xehanort as part of his plan to forge the legendary χ-blade, capable of unlocking Kingdom Hearts itself. Ventus confronts Master Eraqus about this, who attempts to destroy him to stop the χ-blade's creation, but falls at the hands of Terra and Master Xehanort, the latter whom reveals his true intentions afterward.
Terra, Aqua and Ventus eventually battle both Master Xehanort and Vanitas in the Keyblade Graveyard, where they each meet their fates: Terra is possessed by Master Xehanort and transformed into the new Xehanort, but the new Xehanort is defeated by the Lingering Will, Terra's armor animated by his lost mind; Ventus is possessed by Vanitas, but he, Mickey and Aqua fight Vanitas together, Ventus mentally, and Aqua and Mickey physically, but though they manage to destroy Vanitas, the χ-blade and wipe the Unversed from existence, Ventus's heart is damaged and eventually makes contact with the heart of a young boy named Sora, merging with it; Aqua places Ventus's now-comatose body to rest inside Castle Oblivion, and while looking for Terra, finds Xehanort in the Radiant Garden, where he was sent following the χ-blade's destruction, and battles him in an attempt to rescue Terra. During the ensuing fight, Terra temporarily regains control of his body, but Master Xehanort quickly retakes control and attempts to force Terra's heart out of the body, but darkness spills forth and Xehanort starts to vanish.
As Xehanort begins to sink into the Realm of Darkness, Aqua follows and sacrifices her armor and Keyblade to save Terra's body while she remains in the darkness. Xehanort, now with amnesia, is found by the sage king of Radiant Garden, Ansem the Wise, and taken in as his apprentice. Mickey passes his Mark of Mastery Exam under his master, Yen Sid, and is awarded the title of Keyblade Master. Inside Xehanort, Terra continues to counter Master Xehanort, vowing not to stop until he has foiled Master Xehanort and expelled him from his body.
Sora's Journey Begins
Ten years later, the story focus shifts to Sora, Riku, and their friend, Kairi. The first game shows how Sora is separated from his friends when their world, Destiny Islands, is invaded by creatures known as the Heartless. During the invasion, Sora obtains the Keyblade, as Ventus's heart is still within him. Soon after finding himself in Traverse Town, Sora meets Donald Duck and Goofy, two emissaries from Disney Castle searching for the "key" per instructions from Mickey, their King. The three band together and travel to different Disney-themed worlds, sealing the Keyholes to the hearts of the worlds to prevent more Heartless invasions.
Along the way, they encounter a group of Disney villains, led by Maleficent, who is controlling the Heartless. Though they eventually defeat Maleficent, the three discover that Ansem, who has apparently succumbed to the darkness, is the true mastermind. Ansem's plan is to open the door to "Kingdom Hearts", a door which holds the hearts of all worlds, in the hopes of attaining eternal darkness. Sora, Donald, and Goofy defeat Ansem, and seal the door with the help of Riku and King Mickey on the other side of the door.
After sealing the door to Kingdom Hearts, Sora, Donald, and Goofy begin searching for Riku and King Mickey, finding themselves heading towards a mysterious fortress called Castle Oblivion. Upon entering the fortress, they begin to lose their memories. The three encounter a group of dark hooded villains (Marluxia, Larxene, Axel, and Vexen), part of a mysterious Organization. In the castle, Sora learns his memories have been manipulated by a girl named Naminé who was being held captive by the Organization. Sora, Donald and Goofy fight their way through Castle Oblivion and eventually encounter and destroy Marluxia, the lord of the castle, thus freeing Naminé from his grip.
To allow them to regain their memories, Naminé puts Sora, Donald, and Goofy to sleep for a year; in the process, they must lose the memories of their experience in Castle Oblivion. At the same time, Riku finds himself in the basement of the castle and begins traveling to the first floor with the aid of King Mickey. Riku fights both the Organization's members (Vexen, Lexaeus, and Zexion) and the darkness in his heart, particularly the remains of Ansem, learning to accept his dual nature between light and dark. Allying himself with the mysterious DiZ, Riku aids him in keeping Sora and Naminé until the former is awaken. However a snag in the plan over the year forces Riku to go after a young man named Roxas, the thirteenth member of the Organization to enable Sora's reawakening. Roxas, who had defected from the Organization earlier on after doubting their true motives and desiring to meet Sora in person, encounters Riku and defeats him easily at first, but Riku succumbs to the Darkness inside him and takes on the appearance of Ansem. With his newfound power, Riku subdues Roxas and takes him to DiZ, but the ordeal leaves him stuck in Ansem's form. With their plan successful, DiZ orders Riku to eliminate Naminé, but Riku refuses, and instead lets Naminé go with Axel, who turned on Marluxia and chose to aid Sora in Castle Oblivion and who was Roxas's best friend.
Xehanort Revealed
Seven days after Riku's success, Sora, Donald, and Goofy awaken in Twilight Town, with no memory of their time at Castle Oblivion, and resume their search for Riku and King Mickey. They learn of the Nobodies and are re-familiarized with their leaders, Organization XIII (Xemnas, Xigbar, Xaldin, Saïx, Luxord, and Demyx). Afterwards, Maleficent is resurrected and joins with Pete, another Disney villain, to find a new base of operations before resuming her quest for power and revenge on Sora. Sora once again travels to many Disney-themed worlds and resolves the troubles caused by not just Maleficent and Pete, but also Organization XIII whose members refer to him as Roxas. Meanwhile, Kairi is kidnapped by the Organization. They meet King Mickey, who reveals that the "Ansem" Sora defeated was actually the Heartless of Xehanort, who, sometime after being found by Ansem, betrayed his master and stole his name, and since then, Ansem the Wise has been missing. Furthermore, they learn that Xemnas, the leader of the Organization, is the Nobody of Xehanort.
The three arrive at the headquarters of Organization XIII, The World That Never Was, with their own artificial Kingdom Hearts looming overhead, crafted from all the Heartless Sora has defeated, with Sora defeating all the Organization members as he is finally reunited with Kairi and Riku, who reveals Roxas as Sora's Nobody (as well as Naminé as the Nobody of Kairi) that he captured by willingly assuming the form of Ansem so that Roxas could be defeated and absorbed back into Sora. While this occurs, Mickey finds DiZ, revealed to be a disguised Ansem the Wise, with a device to digitize some of Kingdom Hearts' power; as the others arrive, the system overload causes the machine to explode with Ansem consumed in the blast, apparently killing him, with Riku restored to his original state. At the top of the castle, Sora and his friends battle Xemnas, who uses the remnants of Kingdom Hearts to fight them. Refusing to accept defeat, Xemnas separates Sora and Riku from the others to fight them in a final battle that ends with his demise. Though trapped on the other side, Sora and Riku are able to return to their world, via the Door to Light, which was mysteriously triggered from Sora's heart.
The Key that Connects Everything
With the threat of the Heartless and Nobodies apparently over, Mickey, Donald and Goofy return to Disney Castle, where they stumble across a mysterious message written in Jiminy Cricket's Journal, "We must return to free them from their torment". Mickey digitizes the journal and creates a Sora made from Data to uncover the mystery. Following the journal's debugging, Mickey and Data-Sora find the source of the sentence: it was a message from Naminé, who integrated a data version of herself into the journal to inform Sora that he is the "Key that connects everything", and it is his duty to save those connected to his heart from their suffering, namely the real Naminé, Roxas, Xion (a replica of Roxas created by Xemnas as a fail safe in case Roxas or Sora proved useless to the Organization), and Axel, as well as Terra, Aqua and Ventus. Mickey sends Sora a bottled letter about this.
Back in the Realm of Darkness, Aqua stumbles across Ansem, who survived the explosion of the Kingdom Hearts Encoder, which sent him there. Ansem, with few of his memories remaining, fills Aqua in about his experiences with Sora, which brings Aqua to tears of joy, knowing Sora will come and rescue her and her friends. On Destiny Islands, Sora makes the decision to venture out to find and rescue his predecessors from their fates.
However, after successfully locating Aqua and Ventus's heart (thus leaving only Terra to be found), Mickey also receives the shocking news from Yen Sid that the destruction of Ansem and Xemnas has led to the return of Xehanort, and this will eventually bring back Master Xehanort. Seeing as Mickey alone is not enough to defeat what is not "a single one of [Xehanort]", Yen Sid orders Mickey to summon Sora and Riku to him to undertake a Mark of Mastery Exam to properly prepare them for their imminent clash with Master Xehanort.
The Return of Master Xehanort
Sora and Riku answer Yen Sid's call and he explains to them the details concerning Master Xehanort's rise and fall into darkness. Though Sora initially believes himself to be capable of beating Master Xehanort without becoming a Master, Riku readily agrees, in order to prove himself worthy of wielding the Keyblade, which makes Sora accept as well. Yen Sid assigns them to enter the Sleeping Worlds, where they are to locate and awaken seven worlds which were revived following Ansem's defeat but remained submerged in sleep, and in the process unlock the Keyhole of Sleep. If they are successful, they will be deemed Keyblade Masters.
But along the way, Sora and Riku become separated and each end up encountering a mysterious young man in a black coat with ties to Master Xehanort and accompanied by Ansem, Xemnas and Vanitas, whom they believed to have been defeated long ago. Ultimately, Sora's journey leads him to the World That Never Was instead of the Realm of Light, where he meets up with Xigbar and the young man himself, who reveals that he is actually Master Xehanort in his youth from the past, who was paid a visit via time travel by the disembodied Ansem, who sent him on a mission to travel to different periods of time to gather various incarnations of Xehanort to build the real Organization XIII. Sora fights his way to the castle, but is intercepted by Xemnas and ends up with his heart damaged, whereupon he falls into a coma and is taken to the castle by Young Xehanort.
Riku tracks Sora to the World That Never Was and finds him in the castle, where he battles and manages to defeat Young Xehanort with help from Mickey. Following the fight, Master Xehanort is finally revived, and reveals his true intentions: following his failure to use the X-Blade to summon Kingdom Hearts by pitting a heart of pure light against a heart of pure darkness, he has learned an alternate way of creating the X-Blade, since years before, after the Keyblade War, the X-Blade was shattered into thirteen darknesses and seven lights, and if Master Xehanort creates an Organization out of thirteen incarnations of himself, he will be able to recreate the X-Blade and start another Keyblade War. As Master Xehanort moves in to turn Sora into the thirteenth and final vessel for the Organization, Lea, Donald and Goofy arrive to aid Riku and Mickey, and Master Xehanort is forced to retreat with Young Xehanort, Ansem, Xemnas, Xigbar, Saïx and his six hooded vessels.
Riku brings Sora's comatose body back to Yen Sid, where he dives into Sora's Dive to the Heart and collects the fragments of Sora's shattered heart, in the process battling a corrupted Sora himself and winning. Riku succeeds in waking Sora up, but Yen Sid only names Riku as a Keyblade Master for his efforts. Sora, undaunted by his failure, congratulates Riku and departs to continue his training elsewhere. Lea, meanwhile, shocks everybody present by summoning his own Keyblade, something he had only recently mastered and was unable to use against the Organization.
With the knowledge of Master Xehanort's plans, Yen Sid orders Riku to bring Kairi to him so that she may undergo training with her Keyblade and become their seventh light to counter Master Xehanort's Organization XIII.
The Final Showdown
In order to thwart Master Xehanort's plans, Sora, Donald and Goofy embark on a quest to find Seven Guardians of Light and the "Key to Return Hearts", while Riku and Mickey search for previous Keyblade wielders.