The Enterprise finds an asteroid that contains a generational ship on a collision course with an inhabited planet.
The USS Enterprise is attacked by primitive, chemical-fueled nuclear missiles. The crew of the Enterprise destroy the missiles with the ship's phasers and trace back to their source. Kirk orders the Enterprise to proceed to the source of the missiles at warp factor 3. Meanwhile, during a routine health check, it is discovered that Dr. McCoy has a rare terminal illness, xenopolycythemia, that has no known cure and that he will die within a year. McCoy insists that he can remain in his post, but Kirk immediately informs Starfleet and asks for a replacement.
Arriving at the missiles' point of origin, the Enterprise finds only what appears to be an asteroid, 200 miles in diameter, that is not in any orbit but follows an independent course through the local star system. Upon scanning, it is found that the "asteroid" is an atomic-powered spaceship. There are no living creatures detected, leading Spock to presume that the passengers are dead. More seriously, the asteroid's course, 241-mark-17, puts it on a course to collide with the planet Daran V, a planet with a population of 3.724 billion, in 396 days. The Enterprise charts a parallel course to the asteroid/spaceship.
Kirk and Spock prepare to beam onto the asteroid/spaceship, but McCoy insists on joining them. Arriving inside the asteroid, they find a surface that appears geologically active, with a reddish sky, and are puzzled that the builders apparently wanted the inside of the ship to look like the surface of a planet. The surface is dotted by large cylinders, from which a band of primitive, sword-bearing men emerges, led by a beautiful woman. After a brief melee, Kirk, Spock, and McCoy are captured, McCoy sharing a meaningful look with the woman before he is knocked unconscious.
The beautiful woman introduces herself as Natira, the high priestess and leader of her people. Natira "welcomes" the officers to their world Yonada. The three are taken below the surface, where they encounter a large population of young and apparently healthy people. Natira leads the three into an Oracle Room, where she consults an unseen authoritative entity manifested by a decorative altar and a booming voice. Kirk and McCoy estimate that after 10,000 years in this multi-generational ship, the people no longer realize they are inside a spaceship. The Oracle "punishes" Kirk, Spock and McCoy by shocking them with an energy beam, rendering them unconscious.
The three regain consciousness, although McCoy remains out longer due to his illness, in some sort of guest area. Kirk informs Spock about McCoy's condition. An old man gives them an herb derivative to counteract the effects of the Oracle's attack, noting that many people on the ship have been punished in this way. When they tell him they are not from Yonada, he recalls how he once climbed the mountains, "even though it is forbidden," and found that the world of Yonada is not a planet at all. He dies immediately, apparently from a subcutaneous control device in his temple. Natira comes in, and seems to take a special interest in McCoy. Kirk recommends that McCoy use this to their advantage so that they may learn more about the civilization. The captain is faced with a triple dilemma: either risk violating the prime directive by informing the people of Yonada of their situation, destroy the asteroid with them in it or allow it to destroy Daran V. Natira admits she is in love with McCoy and wants him to stay on Yonada as her mate. She speaks of a final destination that is rich and green and notes that the Oracle has promised they shall reach it "soon." When McCoy tells her he has only a year to live, she tells him even a year with him will make her happy.
Spock and Kirk make their way to the Oracle Room, and Spock recognizes the writing as that of the Fabrini, a civilization wiped out 10,000 years ago, when their star went nova. He also sees a symbolic map of the Fabrina solar system. Prior to dying out, the Fabrini had lived underground to protect themselves. They had also built this spaceship Yonada and programmed the Oracle of the People, which is actually a powerful computer, to take it to another habitable planet. The people of Yonada are their descendants. Spock manages to open the door, and the two conceal themselves in the temple as Natira enters. She asks the Oracle for permission to marry McCoy. The Oracle grants it, so long as McCoy agrees to join the Fabrini and submit to the instrument of obedience. As she's leaving, however, the Oracle discovers Kirk and Spock and zaps them. They are arrested and sentenced to death.