Picard and the crew are concerned when an unexplained series of mechanical malfunctions occurs aboard the Enterprise. First, a runaway train suddenly appears in the Holodeck, then the ship takes itself into warp and locks out all propulsion access. Determined to regain control, Picard orders an emergency core shutdown, but before Geordi can carry out the order, the ship takes itself out of warp. An investigation reveals the Enterprise somehow protected itself — it would have exploded just a moment later if it didn't go to warp.

Wondering how the ship was able to do this, Geordi and Data investigate and discover a complex network of nodes and circuitry forming at several points on the ship. When Geordi attempts to examine one of the nodes, it defends itself by emitting a force field. Since the nodes appear to converge in the Holodeck, Riker, Data, and Worf investigate, and are shocked to find several different programs running at once, with characters from all of them riding as passengers on a train. Data attempts to depolarize the power grid, but the characters stop him, then change the train's direction and force Data, Worf, and Riker to leave. At the same time, the Enterprise slips back into warp, and Picard realizes he may not be able to stop it. Data then deduces that the ship is somehow forming its own intelligence.

Data believes that the computer's ability to see, talk, and even reproduce somehow enabled it to go beyond those capacities to learn to think for itself. Since the Holodeck appears to be the focal point, Data and Worf return to the train with Troi. They notice the recurring image of a three-dimensional molecule, and Troi questions a hitman who protects a gold brick and tells her he has to get to Keystone City. The train reaches its destination, and Troi follows him to a brick wall, where he inserts his gold brick and announces that he is laying the foundations. Meanwhile, Picard and Geordi detect strange activity in Cargo Bay Five, where Geordi discovers a glowing shape that looks like the molecule image.

Back on the Holodeck, Data depolarizes the power grid. Suddenly, the ship starts to shake, and Geordi orders Data to stop, then later surmises that the ship is protecting the object being created in the Cargo Bay. Troi adds that the characters in the Holodeck could represent specific aspects of the ship, and Picard encourages her to interact with them again in hopes of gaining control. This time, Troi, Data, and Worf cooperate with the characters, and power and life support systems are immediately restored. Picard and Riker notice that the ship has arrived at a white dwarf star and transports vertion particles back to Cargo Bay Five via a tractor beam. When the supply of particles is exhausted, the "molecule" in the bay goes dark, the Holodeck characters become distraught and the train derails.

The entire ship shuts down, and Picard and Riker join Geordi in the Cargo Bay. They examine the "molecule" and realize that the Enterprise is attempting to create a lifeform — one that will die if more vertion particles aren't found. Abruptly, the ship starts moving again, this time in the direction of another white dwarf. Unfortunately, the trip will exhaust the ship's oxygen supply, killing everyone unless something is done. Picard realizes their only hope is to create an artificial source of vertion particles. But while Geordi believes this can be done in a nearby nebula, Troi, Worf, and Data must somehow convince the Holodeck characters to change direction. Amazingly, they manage to do so, the particles are created, and the entire scene, including the mysterious new lifeform which has now formed, disappears into space, bringing the U.S.S. Enterprise back to normal.