Sent to investigate the destruction of several planetary systems, the U.S.S. Enterprise discovers a crippled starship, the U.S.S. Constellation, floating in space. Commodore Matthew Decker is the only one left on the ship. Kirk and Scotty remain on board the Constellation to try and repair the starship, while McCoy beams Decker aboard the U.S.S. Enterprise.
Decker informs the crew that a giant robot ship, a planet-eating machine made by a long-dead alien race, is roaming the galaxies, consuming all in its path for fuel, including whole planets. When Decker challenged it, the "berserker," as he calls it, attacked. Decker beamed his entire crew to the planet's surface below, only to have the robot consume that planet, killing the Constellation's entire crew.
When the "berserker" returns, Decker, consumed with guilt over the loss of his crew, pulls rank on Spock and takes control of the U.S.S. Enterprise. He seems determined to destroy the machine, even at the cost of another ship and crew. Kirk, still on board the Constellation, contacts Spock and supports his claim that Decker is exhibiting suicidal behavior and is therefore unfit to command. Thwarted, Decker steals a shuttlecraft and flies it down the 'throat' of the giant robot ship, killing himself.
Realizing that Decker's idea, on a larger scale, might work, he sets the Constellation to self-destruct and send it after Decker's shuttlecraft. Due to a transporter glitch, Kirk barely makes it back to the U.S.S. Enterprise before the Constellation explodes, destroying the planet killer in its path.