This contains a lot of huge spoilers for several of the games.
I wrote this myself based on my own experience with the games I've played. I can't explain every single thing about everything but this should generally get the point across of who this character is if you haven't seen him before.
Chain of Memories happens after Kingdom Hearts 1 and before Kingdom Hearts 2. It explains why Sora woke up in a glass egg with some of his memories missing in Kingdom Hearts 2. 356/2 Days picks up just before Chain of Memories begins and goes on after it ends, bridging the final gap between Chain of Memories and Kingdom Hearts 2.
In Kingdom Hearts 1, Sora was trying to find his friends, Riku and Kairi. While doing so, he got caught up in a much bigger plot of trying to stop Ansem (aka, Xehenort's Heartless) from taking over Kingdom Hearts and destroying all of the worlds. He succeeded, but Riku ended up trapped on the other side of the doorway leading to Kingdom Hearts, along with King Mickey.
Sora sets out to find them and is lead by Pluto to Castle Oblivion. I don't know if the remakes explain why this happened but in the originals, it just doesn't. I've always assumed it was Zexion in one of his shape-shift forms since they apparently never caught up to Pluto or got to read the note he was carrying. In fact Pluto is never seen again until Kingdom Hearts 2.
Meanwhile, the Organization is having some internal affairs issues. Marluxia decided to rebel against them, and it never bothers to say why in any of the games I have but since 3D later reveals that Xemnas was deceiving all of the members and using them to meet his own ends, does that mean Marluxia's actually a good guy? Anywho, he is trying to take over the Organization and he wants to use the power of the Keyblade in order to do it, but the Keyblade can only be used by its chosen wielder--- in this case, Sora. Since Marluxia can't use the Keyblade itself, he wants to use Sora to wield it for him. He whips up a plot to use Namine's power over Sora's memories to brainwash him.
Larxene is on Marluxia's side. Axel is pretending to be a fellow conspirator because Saix had suspicions about the two of them and sent Axel in undercover to obtain proof that they were traitors and then eliminate them.
The other three members of the castle, Zexion, Vexen and Lexaeous, are loyal to the Organization. They know how powerful Sora is and are afraid they won't be able to stop Marluxia if Sora is under his control.
But Riku wanders in. Unlike Sora, he wasn't invited. Vexen says this is because Riku's existence "resonates" with Sora's, and it's mentioned in 3D that their destinies are intertwined. Apparently you don't have to actually eat a paupu fruit to make it work, just throw one at your best friend's face and if he catches it you're together for life.
Riku is stronger than Sora and would have had the Keyblade if he hadn't fallen to the power of darkness, so Vexen decides that if Marluxia is going to use Sora to fight for him, he and the others will use Riku. Riku obviously is not going to destroy his best friend, so...
Vexen creates a replica of Riku.
This replica is described as "fake in every way" and I suppose it is some sort of magical mechanical doll. It's different from what Xion is in 365/2 Days because he doesn't turn into crystals when he dies. Also Xion was created from "memories", so taking those memories apart in order to wake Sora up made her cease to exist except in the disconnected memory links in Sora's heart. The Riku replica was created from "data", not memories. If you want to kill him, you have to do it the old fashioned way.
In 365/2 Days, Vexen says he considers his Xion replica to be superior to his Riku replica. When I played the game I came under the impression that the Riku replica came first, sort of like a prototype/test model, and Xion was the perfected version that came afterward. To give his empty puppet a sense of self, Vexen names her No. I, which when rewritten as an Organization name becomes Xion. He doesn't give his Riku replica a number because he's disappointed with how it turned out and finds it "unworthy of a name." Vexen is very proud and when the plans he had for his Riku replica didn't materialize as he wanted, Marluxia and the others humiliated him because of it and from then on he has taken out his anger on his Riku replica and made him suffer because of Vexen's own failures. Riku is referred to either as "the real Riku", or more commonly as "Sora's Riku," while the replica is referred to as "Vexen's Riku" (between Vexen and Sora, "Your Riku"/"My Riku" wink . That is the only name he has.
Riku and his replica are physically identical with the same body, same voice, same abilities. They can both summon Soul Eater and use Dark Mode. The primary difference between them is that Riku is trying to get away from the darkness that once corrupted him and the replica embraces it. They don't have the same personality, either.
In the beginning, the Riku replica is loyal to his master and will do anything Vexen asks him to do. He says he thinks he's better than the real Riku but from the moment he was born, the Organization members teased him about being fake and constantly compared him to the real Riku, so he feels he has to prove that he's not inferior to the original.
The replica fails to take down the real Riku and Marluxia and the others tease Vexen about how his copy pales in comparison to the original. To prove them wrong, he agrees to their proposition to have have Namine rewrite the replica's memory so that he believes he's Riku and "remembers" the events of KH1 and growing up with Sora and so on, plus all the added bits that Namine made up as part of the plan to brainwash Sora. The replica doesn't want this to happen because he sees himself as a separate person from Riku, and he doesn't even like Riku either. His memory is altered against his will.
With his memory rewritten, he thinks he's really Riku, and because of Namine's made-up nonsense, he believes he's in love with her. He fights Sora for the right to have her and Namine becomes afraid that he'll really kill Sora --- something the real Riku wouldn't do, but Namine is Kairi's shadow; she can imitate the relationship between Sora and Kairi or between Riku and Kairi, but she cannot reconstruct the bond between Sora and Riku. Also, even with a new memory the replica doesn't really act the way the real Riku would, probably because Kairi didn't know Riku as well as Sora did (Kairi once made a joke about leaving Riku behind when they were going to run away from home using the raft).
To protect Sora, Namine breaks the Replica's heart. Larxene says that destroying his heart turned him back into a normal doll again so now he's out of the picture. Namine feels bad about what she did, so while Sora is gone to fight Marluxia, she fixes the Replica's memory enough to bring him back to life --- err, restart him? She's captured by Marluxia before she can finish so he wakes up with half of the memories of being Riku for real and half of the memories of being a replica. He can't remember who made him or why or when but he knows the memories he has of Sora and Namine are not real, but in those memories he once made a promise to Namine that he would protect her. Even if it's fake, he wants to keep that promise, so he helps Sora defeat Marluxia.
Afterward, he takes his leave. Sora tries to make him stay and tells him that it doesn't matter if he's fake, he still has his own memories and is still his own person, but the replica just tells Sora that he's a nice person and he doesn't have to be real to see that, and walks away.
Axel shows up and convinces the replica that he can be unique from Riku if he obtains a power that Riku doesn't have. Although Zexion was not one of the traitors, Axel was sent by Saix to eliminate him anyway because guess what? Saix is taking over the Organization too, and Zexion found out about it. Axel doesn't need any help defeating him since Zexion was already injured from being beaten by the real Riku, but Zexion has power over illusions and is a shape-shifter. If the replica absorbs Zexion's power, he obtains the power to change his appearance to anything he wants. He won't have to be a copy of Riku anymore.
Xion created her own identity by changing her appearance, but that won't cut it for Riku's replica. He knows that changing what he looks like doesn't change his past or change who he is; in spite of this new power, he's still "empty inside." He wanders around alone for a while trying to find some meaning to his existence and finally decides to challenge the real Riku one last time. It is a battle he doesn't intend to win.
Riku defeats him but since the replica won't back off this time, Riku is forced to kill him in order to defend himself. Riku was annoyed by him back when they first met but eventually came to sympathize with him. At the end Riku says a few kind things to him and stays with him until he fades away into the darkness.
This character has always been very dear to my heart because I can relate to him in many ways. If I were turned into a KH character and stuffed into the game, this is who I would be.
In the manga version of the story, he does not die at the end and his conflict with Riku is minimized. He runs away at the end with a hobo stick over his shoulder and is followed around by a collection of failed Vexen replicas.