Ralph takes pity on Vanellope the glitchy-racer when he sees that they have a lot in common. Kicked out of sugar land by the others, she lives alone in an abandoned level-mine - the Diet Coke mountain. He helps her break into the main factory and design her own car, then builds a track in the mountain and teaches her the basics of karting.
King Candy soon finds Ralph and begs him not to let Vanellope race -- if she wins, she'll be in the list of racers for gamers to choose -- and when they see her 'glitching' they'll report a fault on the machine. The others will be able to leave and get out to central station, but glitches are unable to leave their machine, and she'll 'die' when it is reset. He hands back the medal to Ralph.
Ralph destroys Vanellope's kart, becoming a proper bad guy for the first time, then when their friendship is over he returns to his own game -- only to find that it has been scheduled for repair. All the citizens have left the apartment block -- just when Ralph can finally be allowed into the Penthouse suite. He looks out the glass into the arcade, and can see the Sugar Rush machine -- and Vanellope's image as one of the main graphics on the side. He knows something is fishy and returns to the game.
Meanwhile, Fix-it-Felix Jnr and the Captain from Heroes's Duty are in Sugar Rush, hunting the bugs, when they are arrested and thrown into the "fun-geon". Ralph shows up to break them out, and with Felix's help they restore little Van's kart, and then set her free as well.
The big race starts, and Vanellope is not at the starting line … but she manages to join after the lights go green, and quickly makes up a good distance on the others. Approaching the 3 leaders, the mean girls, she using her glitching powers to dart through them, leaving them to fall away and not make a huge jump to the other side of the track.
Next up she wrestles with King Candy who knows all the tricks and shortcuts of the course. They tangle, and with her glitching we see the King for who he really is -- Turbo, the dodgy sprite who changed games many years ago. He soon gets eaten by a bug.
Just then the bug nest erupts and the creatures start to devour the game. All the characters make a run for it, but Vanellope can't follow them. It's up to Ralph to save the day.
He gets over to Diet Coke mountain to use the floating Mentos to create a light beacon -- but King Candy returns to stop him, in a hybrid flying bug form, and snatches him up and away. Ralph breaks free and falls - and breaks through the Mentos. They mix with the Diet Coke to create an explosion. Vanellope shows up just in time to save Ralph from falling into the lake.
The bugs are drawn towards the light -- including King Candy, where they are all devoured.
Ralph clears a space around the finishing line allowing Vanellope to cross it, and the game is reset -- we see that King Candy messed with the workings of the machine, and now the Racers' and the Citizens' memories are restored - and we discover that Vanellope is in fact the Princess. But she is very humble, and elects to become the President of a Democracy instead, and forgives the other cruel racers.
Later, Felix and the Captain are married. Vanellope is a favourite with the gamers, and she still keeps her glitching ability which the fans love.
Ralph has returned to his game and it's back in working order, and now the apartment block residents are nicer to him. He has managed to create a little house for himself -- and also invited characters from broken games to come and join him. They have even designed a new mini-game level featuring the others, which is a big hit.
...And he likes nothing more than looking over at the Sugar Rush game when he is on top of the apartment block (ready to be thrown off) to see his little friend winning her races.