Lieutenant Commander Data, while practicing acting in an Enterpriseholodeck, is surprised when the Orient Express train drives through his set of Shakespeare's The Tempest. Later, the Enterprise suddenly jumps into warp on its own; later investigation shows that the ship had been undergoing a deadly theta flux distortion, and only by jumping into warp was the ship saved from destruction. Data postulates that this may be a previously unknown safety feature of the Enterprise, and he and Chief Engineer La Forge investigate the ship, soon discover one of several small nodes connecting the various ship systems within the ship, all protected by their own force fields. The bulk of the nodes connect to the holodeck, and the crew go to investigate.
Inside, they find themselves on the currently-running Orient Express simulation, where several random characters are interacting in strange manners. Data attempts to shut the program down to no avail. As they watch, a gangster character shoots and kill's the train's engineer, and the conductor pulls on a cord; the Enterprise is suddenly sent into warp again. On leaving the holodeck, the crew finds many more of the nodes about the ship, connecting all the major systems save the main computer; Data believes the formation of the nodes are similar to the human brain's structure, and posits that the Enterprise is developing its own intelligence.
Data, Worf and Counselor Troi return to the holodeck to learn more. The gangster character takes a brick from the engineer and gets off at the next stop - Keystone City - which the three follow. The gangster places the brick in a wall, completing it; at the same time, La Forge detects a power surge in a cargo bay and finds a strange object being constructed atom by atom. Data attempts to access the holodeck circuits, but this causes the ship to shake violently. Troi believes that the characters are trying to act out various roles in the creation of a new entity, and Captain Picard agrees with letting the simulation run its course and the object in the cargo bay, considered to be a new lifeform, to develop.
On the holodeck, Data, Worf, and Troi return to the train, and find it is heading for Vertiform City, its final destination; meanwhile, the Enterprise has arrived at a white dwarf star and has started the collection of vertiron particles from it, feeding them into the new object in the cargo bay. However, the supply of particles from the star is limited, and the object's glow starts to wane; on the holodeck, the simulated characters have a shocked response, and the conductor pulls the emergency brake, which causes not only the train to stop but cause power to drop all around the Enterprise. La Forge determines without new vertiron particles, the lifeform will die out.
Suddenly, the Enterprise re-enters warp, diverting power from all other ship's systems, including life support, to maintain its top speed; the conductor on the holodeck simulation states they are now heading for New Vertiform City. With time short for the crew, Data suggests to the conductor that he knows a shorter route, and gains access to the train's engine, from which he can control the Enterprise's course. With La Forge's help, Data directs the ship to a nearby nebula, and subsequently fires a modified torpedo at it, causing the generation of numerous vertiron particles. The ship collects these and completes the lifeform, while the simulated passengers on the train celebrate their arrival. The completed lifeform rises on its own and leaves the cargo bay; the Enterprise returns to normal operation. After verifying their systems have returned to their control and return to their previous mission, the crew postulate on the nature of this new lifeform.