The Federation starship Voyager's emergency medical hologram (EMH), the Doctor (Robert Picardo), is activated and the ship's computer tells him that there is a ship-wide emergency. He scans the ship for the crew and learns that they were all forced to abandon ship. After making his final log entry, the Doctor goes to deactivate himself, but suddenly Chief Engineer B'Elanna Torres (Roxann Dawson) enters the room.
Torres explains that the crew had escaped through the lifepods and she and Captain Janeway (Kate Mulgrew) stayed behind to stop a warp core breach caused by a Kazon attack. Torres says that the Captain is injured on the bridge, and tells the Doctor to go and help her. The Doctor now finds out that there are new holo-emitters installed throughout the ship, enabling him to leave Sickbay.
After the Doctor helps the Captain, he is summoned to the Mess Hall, where ship's chef and morale officer Neelix (Ethan Phillips) is under attack from a Kazon. During the struggle the Doctor discovers that he himself is bleeding and is astonished to feel pain.
While the Doctor is treating his cut, the ship's computer insists that the Doctor is actually Dr. Lewis Zimmerman, the human who created the Emergency Medical Hologram. When the Captain tells the computer to shut down all of the ship's holographic systems, Neelix, Torres, the Kazon soldier and she vanish, but the Doctor remains intact.
Shortly after, Dr. Zimmerman's assistant, Lt. Reg Barclay (Dwight Schultz), materializes. He informs the Doctor that he is in fact not an EMH but is actually Zimmerman himself and that Voyager is really part of a program to test effects of long-term space travel. Barclay tells the Doctor that the Holodeck is affecting his mind and that radiation is slowly killing him. In order to save himself, the Doctor must destroy the holographic Voyager.
As the Doctor begins to believe Barclay and is about to destroy the ship, Voyager First Officer Chakotay (Robert Beltran) appears, ordering him not to believe anything he has seen. After Chakotay stops him from destroying the ship, it is revealed that the real Voyager ran into an anomaly that caused the Doctor's program to malfunction; nothing that happened before Chakotay walked into the holodeck was real. Later, as the Doctor recovers in sick bay, "reality" takes another twist, revealing that the Doctor is still caught in a simulation. Janeway walks into the holodeck and takes the Doctor by the shoulder, bringing him back to true reality.
Writer Brannon Braga intended for the dilemma faced by the Doctor in this episode to mirror the philosophical work of René Descartes, in particular his famous maxim "Cogito ergo sum" ("I think, therefore I am"). He later stated that the episode was his favorite of all the scripts that he had written for Star Trek: Voyager, and executive producer Michael Piller likewise described it as "a fascinating episode."