In 2285 at Starfleet Academy, Admiral Kirk is busy training new cadets. Among the cadets is Saavik, a young protégé of Spock's, who feels that she has failed the Kobayashi Maru — a no-win scenario test used to evaluate potential commanders. Kirk advises the young Vulcan that all commanders at some point must face a "no-win" situation. Saavik, displaying her willingness to become a reliable commander, pilots the U.S.S. Enterprise out of spacedock on a routine cadet training exercise.
Meanwhile, Dr. Carol Marcus, an old love of Kirk's, and her son, David, complete the final computer simulation of the Genesis project — a program designed to grant life where there is none — on the space laboratory Regula I. However, Dr. Marcus is concerned that Genesis could also be used as a weapon. At the same time, the U.S.S. Reliant arrives at Ceti Alpha VI with a mission to check for signs of life on the planet, a possible test site for Genesis. Curious, Captain Terrell and Chekov beam to the surface where they are confronted by Khan Noonien Singh, the former tyrant of Earth's Eugenic Wars, exiled to the planet in 2267 by Captain Kirk. With the aid of mind-controlling Ceti eels implanted in Terrell and Chekov, Khan gains control of the Reliant.
At the Regula I space laboratory, Dr. Marcus is contacted by the Reliant. She is told that Ceti Alpha VI has met the conditions required for testing of the Genesis Device and the Reliant will therefore now take the Genesis Device, months before the scheduled test. Furious at this encroachment by Starfleet, Dr. Marcus contacts Kirk on the Enterprise, expressing her outrage at the Admiral and Starfleet for their militaristic intentions. Although Kirk, McCoy, Uhura and Sulu are leading a training mission for the Starfleet cadets, the crew decides to head to Regula I to investigate Dr. Marcus' complaint. When they arrive, the Enterprise is inexplicably fired on by the Reliant. Khan finally reveals himself from the bridge of the Reliant, demanding that Kirk give him the Genesis information. Kirk pulls a brilliant bluff, allowing the Enterprise precious computer time to secretly lower the shields of the other starship. The Enterprise then damages the Reliant, forcing the captured starship to withdraw for the moment.
When Kirk and company board Regula I, the crew is shocked to find that the scientists have been tortured and killed. They follow transporter traces to the interior of the planetoid below, finding Chekov and Captain Terrell alive and seemingly well in an underground dwelling. Just then, David Marcus pulls a surprise attack on Kirk, believing the Admiral to be the cause of all of the station's trouble. Carol is then forced to reveal that Kirk is David's father. Surprised and bewildered by the sudden turn of events, Kirk and David form an uneasy truce. Carol then shows Kirk and crew the Genesis torpedo.
Unbeknownst to Kirk and the rest of the group, Terrell, still under the influence of the Ceti eels, secretly gives Khan the coordinates of the torpedo. Khan quickly beams the valuable device aboard the Reliant, then orders Terrell to assassinate Kirk. Terrell, however, cannot bring himself to kill the Admiral. Instead, Terrell turns his phaser on himself. When Chekov also resists Khan's orders, the Ceti eel exits his ear and is subsequently destroyed. Khan, thinking he is stranding Kirk and his crew on the planet, departs Regula I. Carol Marcus then shows Kirk the Genesis cave, where an entire biosystem has been created. She explains that this amazing breakthrough means that barren planets can now be made safely habitable for colonists. Kirk, impressed but concerned for his crippled starship, regains contact with the Enterprise and has the landing party and scientists beamed aboard.
Meanwhile, a repaired Reliant under Khan's control obsessively searches for the U.S.S. Enterprise. Left without warp drive in the previous skirmish, the U.S.S. Enterprise hides in the Mutara Nebula. Kirk then deliberately goads Khan, hoping to throw the former tyrant off balance, while the rest of the crew searches for the Reliant in the static of the nebula cloud. Eventually, the U.S.S. Enterprise is successful in fatally disabling the other starship. But Khan, in his desperate hunger for revenge, arms the Genesis torpedo, knowing full well that both ships will be destroyed upon its detonation. With the Enterprise doomed without warp power, Spock suddenly enters the starship's radiation chamber to repair the warp drive. McCoy tries to stop the determined Vulcan, but Spock takes the upper hand and touches the doctor's forehead cryptically, murmuring "Remember." Just as the Genesis torpedo explodes, destroying Khan and the Reliant with it, the Enterprise zips into warp. The crew and ship have been spared, yet Spock, dying of severe radiation, has willingly given his own life.
With the Enterprise safely away, McCoy tells Kirk to come to the ship's engine room. There, a stunned Kirk must bid an emotional farewell to his dearest friend. Kirk need not mourn, says Spock, for his sacrifice was logical. "The needs of the many outweigh the needs of the few, or the one" he tells his captain. Spock dies, but in contrast to a new birth -- the new living star and planet formed from the nebula and Genesis. A sullen and mournful funeral is held for Spock, and, accompanied by Scotty playing "Amazing Grace" on his bagpipes, Spock's coffin is sent to rest upon the new planet.
With Khan defeated, David Marcus finally makes peace with his father. Kirk and the U.S.S. Enterprise then go to Ceti Alpha V to pick up the abandoned Reliant crew, and the ship heads for Earth. Kirk, though saddened at the loss of his friend, is hopeful for the future, because as Spock once said, "there are always possibilities."