Data and La Forge are enjoying a Sherlock Holmes holodeck program when the two notice that a character programmed to be left-handed was actually right-handed. They call Lt. Barclay to repair the holodeck, but as he checks the status of the Sherlock Holmes programs, he encounters an area of protected memory. He activates it to find the artificial sentient Professor James Moriarty (Daniel Davis) character projected into the Holodeck, who appears to have memory since his creation ("Elementary, Dear Data") including during the period while he was inactive. Moriarty wishes to escape the artificial world of the holodeck and was assured by the crew of the Enterprise-D that they would endeavor to find a way to free him, and is irritated at the lack of results on the part of the crew and their seeming lack of even the tiniest bit of effort. Picard, along with Data and Barclay, attempt to assure Moriarty they are still working on that goal, but Moriarty is dismissive of that.
Moriarty confuses the crew by seemingly willing himself to existence by walking off the holodeck. He explains this to the stunned captain and Data by saying, "I think, therefore I am." Moriarty creates a companion for himself, the Countess Regina Bartholomew (Stephanie Beacham), commanding the computer of the Enterprise-D to place another sentient mind within a female character of the Sherlock Holmes novels. Moriarty then demands a solution to get Regina off the holodeck be devised. He takes control of the Enterprise-D through the computer, insisting that a way be found for her to experience life beyond the confines of the holodeck.
Mr. Barclay shows Dr. Beverly Crusher the holocube which is running Moriarty's reality.
While assisting LaForge, Data observes that LaForge's handedness is incorrect, just as they had experienced earlier. Data determines that himself, Picard, and Barclay never left the holodeck, and everyone and everything that appears to be the Enterprise are part of a holodeck program Moriarty created. Data finds a way to program the holodeck's holodeck to convince Moriarty that he and Regina can be beamed into the real world, though in fact they are only "beamed" in the holodeck's holodeck simulation. Moriarty, satisfied with the ruse, releases control of the ship back to Picard. He and the Countess use a shuttlecraft given to them by Picard to leave the Enterprise and explore the galaxy. Picard ends the simulation and returns to the real Enterprise. Barclay extracts the memory cube from the holodeck and sets it in an extended power device in order to provide Moriarty and the Countess a lifetime of exploration and adventure.
Picard mentions the possibility that the crew's reality may actually be a fabrication generated by "a little device sitting on someone's table". This unnerves Barclay enough for him to test the nature of his own reality one more time, by giving an audible command to 'end program' to any suspected computer. This has no effect.