The Enterprise has received a report of a large cosmic cloud entering the edge of the galaxy and is sent to investigate. The cloud is near Mantilles, the furthermost planet in the Federation. Sensors show the cloud is 800,000 kilometers across and half that in depth. It registers as a combination of matter and energy and they watch as it envelops the uninhabited planet, Alondra, in the Mantilles system. Spock determines that the planet is breaking up and they conclude the cloud is consuming planets. The cloud changes course and now heads toward the inhabited planet of Mantilles. The Enterprise accelerates to intercept before it reaches Mantilles while Spock speculates that the cloud may consume stars as well as planets. With four hours remaining, McCoy advises Kirk to tell the governor, Bob Wesley.
The Enterprise approaches the cloud and Spock determines the cloud has come from outside the galaxy. The cloud envelops the Enterprise and pulls itself in. Phasers prove useless as the cloud absorbs their energy. Large gaseous antimatter chunks approach the ship and impact on the shields. They release an antimatter charge to repel the objects, but Scotty warns that the engines can't maintain the energy output necessary. Meanwhile, Spock speculates that the cloud is alive with components similar to those found in a living organism. McCoy partially agrees and warns that the mists outside are acting as an enzyme working to break down the ship's hull. If the shields fail, the ship will be destroyed just as Alondra is.
As Sulu assembles a schematic of the cloud's construction, Wesley contacts Kirk and informs him that they have no time to evacuate the planet, and they'll save the children. With three hours and 20 minutes left, Spock determines that the cloud needs food and lives on the energy it gains from consuming planets. The antimatter chunks are teeth that break up the larger chunks for digestion. Going over the schematic, they determine that there's another opening near the top opposite where they were pulled in. They proceed toward the exit point through the central core area and reach its entrance point. They're pulled in and find themselves surrounded by protrusions similar to villi. McCoy believes they are in an area similar to the small intestine, and the villi absorb nutrients. Matter impacting on the villi explodes and Spock determines they are made out of antimatter. They're forced to keep the shields up and Scotty warns the engines are undergoing enormous strain. They have only 21 minutes before they run out of power. Arex navigates for the opening on the other side but the power continues to exhaust. Scotty suggests they get a piece of the villi and use its antimatter to regenerate the engines. He rigs a force field box to contain the antimatter, and then they cut off a small piece and beam it aboard into the force field box. Scotty and Kirk take it to the warp nacelles and put it in place with seconds to spare. The engines regenerate and they're able to maintain the deflectors.
With 42 minutes remaining, Spock determines the cloud entity has a brain. He speculates that the course change may represent an intelligence action. They head for the brain and prepare photon torpedoes to destroy it. Spock warns against it, noting the regulations against the killing of intelligent life. Kirk admits they don't know if the creature is intelligent and they have no other choice. Kirk wonders if he has the right to kill the creature but McCoy notes they have no other choice. Spock pinpoints the critical brain areas but warns the area is too vast for their entire arsenal to have any impact. However, if they self-destruct the ship they can kill the creature. Kirk orders Scotty to prepare the engines for self-destruct. Wesley contacts them and informs them that everyone has agreed to let the 5,000 children be rescued out of 82 million people. Wesley's daughter Katie is with him and Kirk assures him that she'll be okay.
Kirk suggests Spock use the Vulcan mind touch to contact the creature. Spock tries to reach out with his mind through the ship's sensors. Scotty completes self-destruct preparations while Spock reaches out with his mind. The entity starts to respond but can't understand the concept of entities so small within itself. He tells it to focus on the planet it's approaching and to realize the 82 million beings are like it. The entity is unable to comprehend them as living beings. Spock mind melds with it completely and the Spock/entity gets up and looks around the bridge. It hesitantly touches Kirk, who has Uhura bring up views of life on Earth. Kirk explains that Earth is their point of origin and has Uhura bring up images of cities, children, and pets. With seconds remaining, the Spock/entity sits back down and the cloud stops. The entity says that it comprehends and sets course away from Mantilles. Spock suggests it return to its point of origin, and despite the fact it's a long journey it agrees. The Enterprise leaves the cloud through the entity's sensory grid and Spock talks of the wonders of the universe he perceived through the entity's memories.