The Enterprise is near the Neutral Zone running a geological survey when three Romulan ships ambush them. Forced to retreat when the ship is damaged, Kirk has Uhura hail the Romulan commander. He claims that the Enterprise is trespassing in Romulan space and refuses to listen to Kirk's denial. The commander points out that violation of the Neutral Zone is a capital offense and cuts off, and the Romulans continue firing on the Enterprise.
An energy field appears ahead and Spock confirms that it is composed of highly charged subatomic particles. Kirk has Sulu taken them into the cloud to evade their pursuit. The Enterprise shakes under the bombardment but Spock assures them that they should be fine if it gets no worse. They come out the other side and lie by for repairs. After beginning the work, the command crew takes a break for a meal. Scotty assures them that repairs will be complete within 24 hours. McCoy offers a toast to their escape but when everyone drinks, the glasses dribble onto their chests. They figure that someone has substituted dribble glasses as a practical joke, even though all of them were equally soaked. Scotty suggests that the synthes malfunctioned and Kirk starts eating. However, his fork wilts and dumps the food into his lap. The rest of the crew chuckles briefly at the incident.
As the repairs continue, someone plays more practical jokes. Spock finds an odd viewer device on his console which leaves black marks on his eyes. The jokes continue escalating and in the mess room, Scotty is attacked by a food synthe that spits out dozens of food items at him, burying him in consumables. M'Ress and Arex look on and uncontrollably chuckle. When Scotty threatens to report them, a cream pie shoots out of the synthe and hits him square in the face.
Kirk finally has enough when someone puts "Kirk is a Jerk" on his tunic. When he complains on the bridge, someone laughs but no one seems responsible. Mist pours out of the turbolift from a lower deck and Kirk and Spock go to investigate. The floor is covered in what Spock confirms is normal fog. He loses his foot and Kirk almost falls as well when he goes to help his friend. They discover that the deck is covered in ice, and the same laughter from the bridge echoes over the intercom. Kirk thinks that he recognizes it but can't place it. Spock realizes that the laughter is the voice of the main computer, and their practical joker is the Enterprise itself.
When they get off-duty, McCoy, Sulu, and Uhura go to the holographic rec room to relax. They go inside just before Spock calls everyone to stations to run a ship-wide system check. Inside Sulu goes to the control panel and brings up a hologram projection of a forest.
On the bridge, M'Ress reports that the missing officers haven't checked in. She sends out another summons but the computer cuts off the intercom in the rec room. The three officers go for a walk and hear the computer laughing, but don't recognize it and dismiss it as a tape malfunction. The computer creates a covered pit ahead of them and when they come along, the fall in. They hear the same laughter and McCoy unwittingly makes several puns about getting to the "bottom" of things and "falling" for their unseen joker's pranks. The computer finds the whole thing amusing and keeps laughing.
Spock asks the computer for their crewmates' location and it refuses to answer, giving childish answers. When Kirk tries to override it, it refuses to respond to voice commands, so the captain orders Scotty to shut down the logic functions.
McCoy and the others climb out of the pit and he complains about being trapped. The computer tells him to "cool off" and then creates a snowstorm hologram, complete with freezing temperatures. Sulu realizes that in the whiteout conditions they can't find the exit door.
When Scotty tries to shut down the main computer, it cuts the gravity everywhere except the rec room. Scotty drifts away and Spock warns that the computer may use more lethal methods to defend itself. Kirk orders Scotty out of the engine room and as soon as he leaves, the computer abruptly restores gravity. Spock analyzes the energy field and concludes that it disrupted the ship computer's higher functions like a virus, giving it a nervous breakdown. When Kirk wonders how they can deal with it, Spock admits that he has no logical solution to the computer's illogical behavior.
In the rec room, the crew members are in danger of freezing. Uhura suggests that they continue in one direction until they come to a wall, and then feel their way around to the door.
A search party discovers that the rec room doors are sealed and scan to confirm the missing officers are inside. As Kirk starts to contact Scotty to take a team to pry open the door, the captain bursts into laughter. The rest of the crew joins in... except for Spock, who winces in pain. He tells the hysterical Kirk that the ship's computer is now filling the atmosphere with nitrous oxide, which causes uncontrollable laughter in most species and migraines in Vulcans.
The gas fills the rec room as well and the three officers continue trying to find a way out, laughing hysterically.
Spock switches to the reserve oxygen, giving them enough fresh air for six hours. The computer has set up an energy-draining field around the rec room, forcing Scotty and his team to use crowbars. Inside, McCoy's legs finally give out and he collapses. Uhura figures that they're close to the wall and prepares to continue... and the snow storm disappears, replaced with a series of hedges. Sulu figures that they're safe, but McCoy realizes that they were close to the wall so the computer created a hedge maze to lead them away. Before the computer can respond, the rescue team gets the door open.
When McCoy and the others return to the bridge, the Enterprise computer takes the ship off-course, overriding the manual controls and taking the starship toward the Neutral Zone. Spock detects the three Romulan ships moving and then notes that the computer is manufacturing something in the shuttle bay. A loud metal object drifts out and then inflates, revealing that it's a scale replica of the Enterprise… 20 times larger than the original. The Romulans hesitates when they see it, and Kirk realizes that the rogue computer is playing the biggest practical joke of all on the Romulans. When they fire on it, they'll be disgraced when all they destroy is the equivalent of a balloon.
When the decoy doesn't respond to their hails, the Romulans attack it and easily destroy it. They realize that they've been tricked and go after the Enterprise, and Kirk suddenly panics. He has Sulu confirm that they're nowhere near the energy field, insisting that he can't handle taking another trip through it. The computer notes his panic and changes course toward the energy field. The Romulans try to pursue them but their weaker ships crack under the stress and they're forced to retreat.
Aboard the Enterprise, the computer starts to falter and it accuses Kirk of tricking it. Kirk, back to his usual self, tells McCoy that the worst thing to do to a practical joker is play a practical joke on them. The Enterprise leaves the energy field and Spock confirms that the computer is back to normal. When they went back through the energy field, it undid the damage done the first time. Uhura monitors the Romulans' internal communications and puts it on the loudspeaker. The Romulans' computers have gone out of control and are flooding the ship with food. Kirk tells McCoy that they'll contact the Romulans and tell them how to undo the damage... after they've had their fun.