A female Vulcan sits in the command chair on the bridge of the U.S.S. Enterprise. While the senior crew and some cadets work at their consoles, the officer, Saavik (Kirstie Alley), makes a log entry, then orders Commander Sulu (George Takei) to project a course to avoid entering the Neutral Zone at the Klingon frontier.
Suddenly, Uhura (Nichelle Nichols) receives a distress call from the Kobayashi Maru, a ship that has struck a "gravitic" mine near Altair VI, inside the Neutral Zone. Despite warnings from both Sulu and Spock (Leonard Nimoy), Saavik orders the ship to enter the Zone in order to beam the survivors aboard. Upon entering the Zone, the Enterprise is confronted with three Klingon battle cruisers, which open fire. The Enterprise is heavily damaged; many of the bridge officers are killed. Saavik has no alternative but to order the surviving crew to abandon ship.
Then the filtered voice of Admiral Kirk (William Shatner) is heard. The bridge viewscreen slides aside, revealing a lighted room beyond. The Kobayashi Maru was a Starfleet Academy test, one Saavik does not believe to have been a fair test of her abilities. Kirk explains that the no-win scenario is a situation every commander may face, and that how one faces death is equally important as how one faces life. Saavik seems ruffled at the advice, but Kirk offers that now she has "something new to think about." As he leaves, McCoy asks Kirk why the Enterprise will not receive an experienced crew. Kirk replies that space exploration should be left to younger crews, a remark that puzzles Uhura.
Outside the simulator room, Spock awaits Kirk's opinion of the cadets' performance. Kirk notes that the trainees wreaked havoc with the simulator room and Spock alike. Spock recalls Kirk's own Kobayashi Maru, noting that Kirk himself took the test three times and that his final solution was "unique." Kirk then thanks Spock for his birthday present, an antique copy of Charles Dicken's "A Tale of Two Cities." Spock then leaves to catch a shuttlecraft to board the Enterprise and await Kirk's arrival--he will later inspect the ship.
Kirk then retreats to his apartment, to be greeted by Dr. Leonard McCoy (DeForest Kelley), who presents him with two gifts; a bottle of finely-aged Romulan ale and a pair of antique eyeglasses. Noticing Kirk is going through a mid-life crisis, he questions whether Kirk really wants to carry on the duties of an admiral, or to be "galaxy hopping" in a starship. McCoy urges Kirk to get back his starship command, and the two share a drink sitting by the fireplace.
Meanwhile, Commander Chekov (Walter Koenig) is on board the U.S.S. Reliant, which is orbiting the planet Ceti Alpha VI. The crew is searching for a lifeless planet to satisfy the requirements of a test site for the "Project Genesis" experiment, a terraforming program proposed to the Federation by a group of scientists. Although Ceti Alpha VI should be incapable of supporting life, Chekov detects a minor energy reading on a scanning device. Chekov and Captain Terrell (Paul Winfield) beam down to the surface to investigate. Upon arrival, they fight their way through a blinding sandstorm until they discover and enter what appears to be a crashed derelict vessel.
They soon discover that the derelict is actually cargo containers assembled together from the S.S. Botany Bay, a ship Chekov remembers all too well. Panicking, he rushes a confused Terrell toward the exit, only to find that a group of strangely-dressed people are waiting outside. Chekov and Terrell are taken prisoner, and their captor reveals himself as Khan Noonien Singh (Ricardo Montalban). He identifies the rest of his group as the remaining survivors of his crew. Khan reveals that 15 years earlier, Captain Kirk exiled Khan and his followers to Ceti Alpha V after the genetically-engineered supermen nearly captured the Enterprise. Khan says that six months after they were marooned, Ceti Alpha VI exploded, destroying Ceti Alpha V's ecosystem and shifting its orbit and position in space. The crew of the Reliant thought they were orbiting Ceti Alpha VI, when in reality they were orbiting Ceti Alpha V instead.
Khan blames Kirk for the death of his wife (presumably Lt. Marla McGivers, an Enterprise crewmember who joined Khan in exile) and plans to avenge her. In order to find out why the two are there, Khan forces juvenile Ceti eels (unpleasant-looking creatures) into their ears. Once inside their victims, Khan explains, the eels wrap themselves around the cerebral cortex of the brain, rendering the victim subservient to any command and to interrogation. Khan explains further: As the eels grow and mature inside the brain, the victim is slowly driven insane, followed later by death. Using Chekov and Terrell as his servant, Khan and his henchmen to seize control of the Reliant and escape Ceti Alpha V.
Under the command of now-Captain Spock, the Enterprise is being used to train Starfleet Academy cadets, and Kirk, McCoy, Uhura, and Sulu come aboard to assist in a short training cruise. Kirk is inspecting the trainee crew, who are learning the ropes on Enterprise. Later, Kirk receives a distress call from Regula I, a research station that is the Project Genesis base. Kirk orders the call to be patched through to his quarters, a communication he is nervous about since he and the head of the Genesis project, Dr Carol Marcus, (Bibi Besch), were romantically involved in the past, a relationship that went sour and resulted in the birth of their son, David who works with his mother on Regula.
A furious Carol asks him why he is taking the Genesis Project away from her. Kirk is confused and incredulously denies having any involvement with it. The transmission becomes garbled and communication is soon lost: Khan is luring Kirk to Regula I by having a now-brainwashed Chekov inform Carol that Kirk had ordered them to take possession of the Genesis Device. The Reliant will be coming to the station in three days to take the Genesis equipment. Carol contacted Kirk to confirm the order, but the signal is jammed by Khan, with only bits and pieces of the message going through. Kirk, after consulting with Starfleet Command, assumes command and orders Enterprise to set a course for Regula I.
While en route, Kirk asks Spock and McCoy to join him in familiarizing himself with the Genesis project. A video, hosted by Carol Marcus explains that the project involves the sophisticated terraforming of dead planets, making them habitable. Because the video was produced a year before, Kirk assumes they've reached "Stage Two" of the project. McCoy asks what the result of using such a device on a living world would be and Spock concludes it would destroy any existing life. McCoy sees the project as a dangerous venture that could be turned into a deadly weapon and is angry at Spock's logical attitude about the dangers. Just at that moment, Saavik calls them over the intercom and tells them that they've made contact with Reliant.
The Enterprise approaches Reliant. Despite being unable to contact Reliant, Kirk is unconcerned at first and is reluctant to raise shields as, Saavik reminds him, regulations prescribe. He orders a yellow alert. The Reliant raises its shields, powers up its weapons, and opens fire. The Enterprise is caught off-guard and is badly damaged. Khan knows exactly where its weak points are, disabling the Enterprises' main energizers and warp core, leaving only auxiliary power on the ship, and mortally injuring several cadets, including Midshipman 1st Class Peter Preston (Ike Eisenmann), Scotty's (James Doohan) nephew. Engines are down, shields inoperative, and there is only enough power for a few short phaser shots, which isn't enough against Reliant's shields.
Khan hails Kirk, who is shocked to see Khan in command of the Reliant. Khan arrogantly announces his plans to destroy the Enterprise, to which Kirk pleads with Khan to take him as prisoner and spare his crew. Khan agrees, but also demands all information on the Genesis Device. Kirk pretends to comply, but he actually has Spock transmit a signal using Reliant's prefix code that causes Reliant to lower her shields. Despite Khan's intelligence he is still very inexperienced with a starship. When he realizes what Kirk is doing he is unable to immediately locate the controls to override the command lowering the shields. With the few shots auxiliary power can give him, Kirk is able to fire at the Reliant, damaging photon control and the warp drive. Khan is reluctant to withdraw, but his followers remind him that Enterprise, with its disabled power systems, can't escape. Both ships limp away for repairs and the match ends in a stalemate. Sulu congratulates Kirk on his victory, however Kirk admits that he'd misjudged the situation and encourages Saavik to quote Starfleet regulations whenever applicable.
Kirk surveys the wounded in sickbay and attends to Peter Preston on his deathbed. With impulse power restored, the Enterprise arrives at Regula I. Kirk assembles a landing party, and Saavik reminds him of General Order 15 barring him from beaming into a dangerous situation without armed escort. They find the station abandoned and several of the station's scientists murdered, and discover Chekov and Terrell, semi-conscious and abandoned inside a storage container. Terrell and Chekov, still quite dazed, relate their experiences with Khan and tell Kirk that Khan is quite insane. When asked where the crew of the Reliant are, Terrell says they were marooned by Khan on Ceti Alpha V. They find that the station's records of the Genesis Device have been erased by the Regula staff. Exploring the station leads them to a transporter that has recently been activated. Checking the coordinates, Kirk realizes they beamed into the Regula asteroid nearby, assuming they'd gone to where Phase II of the Genesis experiments were conducted. Kirk asks for a damage report from the Enterprise. Knowing that Khan is listening to their communications, Spock exaggerates and reports that "by the book, hours would seem like days" and that transporters will be available in two days, hinting to Kirk that they can be beamed back in about two hours.
Using the transporter coordinates, they beam down to the asteroid and materialize inside a chamber. The Genesis Device is there, but before Kirk can move, he is attacked by his and Carol's son, David Marcus (Merritt Butrick), who accuses Kirk of trying to steal Genesis. Carol tries to defuse the situation, but before she can elaborate, the team is threatened by Chekov and Terrell. Terrell and Chekov reveal they are still under Khan's control. and Terrell is ordered by Khan to kill Kirk. Terrell, however, resists Khan and the eel causes him extreme pain. To escape it he turns his phaser on himself and is vaporized. Chekov collapses and the Ceti eel slurps out of his ear and is promptly destroyed by Kirk. The Genesis Device is suddenly beamed away. Kirk grabs Terrell's wrist communicator and taunts Khan, telling Khan that he'll have to come down to the planetoid if he wishes to kill Kirk. Khan, shocked to find Kirk alive and well, vows to leave him marooned on Regula for eternity. Khan cuts off communication with his enemy.
Kirk avoids Carol and David's questions about Khan by asking for food. Carol and David show Kirk, McCoy and Saavik the Genesis cave, which was created by a smaller Genesis Device: deep within Regula a stable ecosystem now exists, having been created in one day. Before Kirk and Carol join them, the two talk briefly about their past relationship and reach a moment of reconciliation.
In the cave, Saavik asks Kirk, who is casually eating an apple, about his performance on the Kobayashi Maru scenario. McCoy tells her that Kirk is the only one to beat the no-win scenario. However, Kirk admits he reprogrammed the simulation. David chuckles and says he cheated, and Kirk qualifies that he "changed the conditions of the test" also citing that he'd received praise for "original thinking" and that he does not believe in the "no-win" scenario of the Kobayashi Maru test. Kirk then promptly contacts Enterprise and Spock says they should prepare for transport. Kirk smiles at a dumbfounded Saavik and asserts that he doesn't like to lose. Saavik questions what happened throughout the transport and Kirk reminds her of Regulation 46A: Spock had modified his report (hours instead of days) to deceive Khan because their adversary may have been monitoring Enterprise's transmissions.
Unfortunately, the Enterprise cannot defend itself against Reliant. Spock suggests the Enterprise set course for the nearby Mutara Nebula, where ionized gases will disrupt the sensors and shields of both vessels, essentially rendering them blind and evening the odds. Khan orders Reliant to pursue, but his crew is reluctant, as they know the shielding and sensor systems will be rendered useless. Khan's second-in-command, Joachim, slows the ship.
Back on the Enterprise, Spock notes that Reliant is reducing speed and seems to be backing away from the pursuit. To ensure that Khan will follow him, Kirk has Uhura contact Reliant and proceeds to taunt his nemesis, saying "We tried it once your way, Khan. Are you game for a rematch? Khan ... I'm laughing at the superior intellect." Enraged by the mockery, Khan acts irrationally and orders full impulse power and despite Joachim's protests, recklessly launches into the pursuit again. The Battle of the Mutara Nebula ensues. Both ships are quite hampered by the conditions whereas in open space Enterprise would have been the more vulnerable vessel.
A game of cat-and-mouse follows. Computer targeting does not function, so both crews must rely on manual firing commands based on their view of the opposing ships on the visual display, which is mostly static. Sulu, being more experienced, is able to make better guesses and inflict slight damage but both vessels largely miss each other.
As they maneuver half-blind around the nebula, suddenly the static on the Enterprise screen clears enough to reveal that the ships are about to collide. They veer apart and narrowly miss colliding, and at such point-blank range even manual firing is sufficient for each vessel to inflict key hits on the other. The Reliant manages to destroy the port torpedo launcher of the Enterprise, which then returns fire and damages the Reliant's bridge, causing an explosion that kills several of the ship's bridge crew including Joachim, whom Khan vows to avenge.
Kirk is nevertheless able to ambush the Reliant because of his superior starship combat experience. When Spock suggests that Khan is inexperienced and is using a strategy based on two-dimensional thinking, Kirk orders the Enterprise to drop below Reliant. Reliant glides past above Enterprise. A shaken, but physically recovered Chekov enters the bridge and offers his assistance. Kirk asks him to go to the weapons station. Khan isn't prepared for Enterprise to descend before he passes overhead and then ascend directly behind him. Reliant is hit with several phaser blasts, and a torpedo breaks off its port nacelle. Reliant is crippled and drifts away, trailing plasma. Most of Khan's crew is killed in the process, and Khan himself is left crippled and barely alive.
In a final attempt to kill Kirk, Khan activates the Genesis Device, knowing that the blast wave from it will destroy the Enterprise and its crew. The Enterprise's warp drive is off-line from the earlier battle, and she cannot escape the large explosion that the device will trigger. Spock exits the bridge and decides to sacrifice his life by entering the radiation-filled engine room and fixing the broken warp drive, while Kirk orders a withdrawal at "best possible speed." On Reliant's bridge, Khan, believing the Enterprise cannot escape the blast, quotes Moby Dick: "From hell's heart, I stab at thee. For hate's sake I spit my last breath at thee." Spock arrives in engineering, only to be stopped by Dr. McCoy from entering a lethally irradiated compartment that is part of the warp drive system. After initially appearing to comply with McCoy, an apologetic Spock nerve-pinches McCoy, and mind melds with the doctor, saying simply "Remember..." He then dons work gloves, enters the chamber, and begins to repair the main reactor. Moments later, McCoy regains consciousness and he and Scotty plead in vain to Spock to stop what he is doing.
Spock is successful and the warp engines come on line just in time, and Enterprise streaks away just as the Genesis Device, and the Reliant, explodes. The Mutara Nebula condenses around the explosion, creating a new planet. Kirk contacts engineering to congratulate Scotty, but he is unconscious due to the radiation he'd been exposed to. McCoy gravely replies that Kirk needs to come down; Kirk notices the empty chair at the science station. A look of complete horror fills Kirk's face as he rushes down to Engineering to find Spock, dying. Kirk calls out for Spock and follows as the Vulcan staggers to the side of the transparent radiation barrier, finally resting against it.
Spock attempts with difficulty to explain to Kirk his reasoning: "Do not grieve, Admiral. It is logical. The needs of the many outweigh ..." to which Kirk finishes, "the needs of the few," and Spock nods. "Or the one ..." Spock states that he himself never took the Kobayashi Maru simulation "until now," and asked Kirk, "What do you think of my solution?"
Kirk, stricken with grief, can't reply. "I have been and always shall be your friend. Live long and prosper." He holds out his hand, in the traditional Vulcan salute, and Kirk presses his hand up to the glass as well, watching as Spock slumps to the floor, and dies. It takes all of his resolve to keep his composure as he sees his closest friend die in front of him.
Spock's funeral is held later, on the torpedo deck. Kirk says a few words in Spock's honor, concluding with a befitting statement: "Of all the souls I have encountered in my travels, his was the most... human." While Scotty plays "Amazing Grace" on the bagpipes, Spock's body is launched in a torpedo casing into the atmosphere of the newborn Genesis Planet.
Later, trying to relax in his quarters, Kirk tries to read his copy of A Tale of Two Cities. He sees that the glasses given to him by McCoy are broken. David visits him and the two reconcile, accepting that they are father and son. Kirk is humbled, especially when David mentions that Kirk had faced death before but never the death of a close and trusted friend like Spock.
On the bridge, the crew and Carol Marcus look at the new world formed by the Genesis Wave. McCoy expresses the feelings of Kirk by saying "He's not really gone as long as we find a way to remember him." The Enterprise sets it's course for Earth, with a stop at Ceti Alpha V to pick up Reliant's crew.
The shot dissolves to various scenes of the ecosystem of the Genesis planet, finally arriving at Spock's photon tube. In voiceover, we hear Spock's voice reciting the Star Trek motto.