Buzz Lightyear and his partner Warp Darkmatter are searching for three missing Little Green Men (L.G.M.), a noosphere-dwelling race working as scientists for Star Command's Universe Protection Unit. They discover the lost L.G.M. in a hidden lab belonging to the evil Emperor Zurg. Buzz and Warp break in and rescue the L.G.M., and a battle breaks out with Zurg's robots while they escape. Zurg triggers the self-destruct mechanism; Warp gets pinned under wreckage and forces Buzz to leave just before the explosion happens, presumably killing Warp.
Stricken with survivor guilt over Warp's death, Buzz refuses a new partner, but is given a Star Command recruit, Princess Mira Nova, whom Commander Nebula trains. With the power of "ghosting", Nova is nearly invincible. Buzz later prevents a well-meaning janitor named Booster from being fired.
In Zurg's fortress, a new henchman called Agent Z has a multi-weapon robotic arm. Zurg learns of a huge orb on the L.G.M. homeworld called the Uni-Mind, responsible for the telepathic link between them; he sends his robots to capture it. The L.G.M. build a robot soldier called XR, who is offered to Buzz as a partner as he can be repaired after any damage. They get a telepathic message about Zurg's attack. When Buzz and XR arrive on the L.G.M. planet, they are confronted by Agent Z, who destroys XR while the Uni-Mind is stolen by Zurg. Unable to think clearly, the L.G.M. rebuild XR, but with a mind of his own. Commander Nebula decides to launch a full-scale assault on Planet Z, but Mira argues that a solo ranger could go to stop Zurg with the prototype Alpha-One. Nebula rejects the idea, deciding to launch the full-scale assault.
Zurg corrupts the Uni-Mind into a "Mega-Ray" to bend everyone to Zurg's will. Mira steals the Alpha-One prototype spacecraft to fight Zurg. Buzz pursues Mira in his own craft, not realizing Booster and XR are stowaways. Eventually, Buzz catches Mira and stores Alpha-One in his spaceship's cargo bay; Booster and XR are then discovered. Zurg's Mega-Ray subverts several planets in quick succession before turning it on Star Command. Buzz, Mira, Booster, and XR discover all of the staff have been suborned by Zurg; they flee in Buzz's Star Cruiser. Zurg uses Star Command's entire arsenal, planting a bomb on Buzz's ship. Buzz and the others escape in the Alpha-One just before the bomb detonates, destroying the cruiser. Zurg presumes Buzz dead.
Booster accidentally causes the ship to crash-land on Planet Z. There, Buzz insists on finishing the mission alone and orders the others to leave. Buzz fights Agent Z, but he is shocked when Agent Z removes his helmet, revealing himself as Warp. Taking advantage of Buzz's momentary shock, Warp incapacitates him and delivers him to Zurg. Warp explains that he faked his death and was secretly working for Zurg for years as a double agent. When Zurg mocks Buzz, Buzz responds by dictating his "final log entry". Mira, Booster and XR hear Buzz's final entry which includes a coded plea for help.
Zurg plans to use the Mega-Ray on Buzz, but XR and Booster intervene in time to rescue him as it fires. Booster and Mira destroy Warp's mechanical arm after Booster lands on him. Buzz and Zurg face each other; Zurg thinks he has won, but Buzz and his three friends corner Zurg and place him under arrest. Zurg escapes through a trap-door. Booster and XR arrest Warp and skydive from Zurg's now-exploding tower. Mira uses her "ghosting" power to push Buzz to the core of the Uni-Mind and restore it to normal, freeing the suborned peoples and leaving Zurg momentarily helpless and apparently destroyed. The unity of the L.G.M. is restored and Warp is taken to prison for treason.
Buzz, having at last admitted that he cannot work alone, creates a new team called "Team Lightyear" with XR, Mira and Booster. They fly to the galaxy and shout "To infinity and beyond!", closing the film.