At the beginning of the episode, Homer is watching the news on Halloween night while eating candy, which ends up being taken by Jimbo and Kearney after they threaten to egg his house. Marge and the kids return home after trick-or-treating with a big haul of candy. Despite warnings from Marge that eating the candy will cause nightmares, Homer, Lisa, and Bart eat from the large candy pile. That night, all three try to sleep, and Lisa is the first to have a nightmare.
In the first of three segments, the Simpsons visit Morocco (which Homer mistakes for Monaco). Homer purchases a severed monkey's hand that will grant four wishes, despite the vendor's warning that the wisher will meet with grave misfortune. Back in Springfield, the family argues in how to use the wishes. Despite Marge's warning to heed the advice of the vendor in not using any of them, Maggie is granted that first wish: a new pacifier. Next, Bart wishes for the Simpsons to be rich and famous. His wish is granted, but the people quickly tire of the family's annoying antics, cheesy merchandise and celebrity treatment. Horrified by these wasteful wishes, Lisa wishes for world peace and Homer is furious. While all countries declare peace and destroy their weapons, the aliens Kang and Kodos realize the human race is "ripe for the plucking" and, armed only with a slingshot and a club, enslave the Earth. At the house, Homer blames Lisa and deems her own wishes to be selfish. She defends herself, claiming that Bart and Maggie's own wishes were also selfish as well and felt that hers was the only selfless one. Soon, Marge catches the people who are angry with the Simpsons and calls for a temporary truce in the family. Determined to make a wish that cannot be twisted, she begs Homer to do the right thing and use the last wish to get rid of the aliens. Much to her dismay, Homer refuses and demands a turkey sandwich, but the turkey turns out to be a little too dry. With all of the wishes used, he goes to throw out the paw, but his neighbor Ned Flanders asks about it. Hoping to see Flanders suffer, Homer gladly hands the paw over. Ned's first wish is to "Get rid of those awful aliens," which is accomplished when Moe chases after Kodos with a board with a nail sticking out of it, then brings the aliens back to the flying saucer. Kodos laments their failed invasion, but Kang assures him that humanity is doomed: they will end up in an arms race creating increasingly bigger boards and bigger nails, which will ultimately "cause the human race to create a board with a nail in it so big to doom mankind." Everyone celebrates and after Flanders wishes to "spruce up the ol' homestead," his house is converted into an opulent castle, making Homer jealous. Lisa wakes up from her nightmare and begs Bart to keep her company. Bart sleepily complies after Lisa gives him a candy necklace. Bart then has a nightmare.
In the second segment, Springfield is held in a grip of terror by Bart, who has omnipotent powers. Bart turns whoever is not happy with his rule into another being, and even history is changed to suit Bart's pleasure. When Homer refuses to turn off a football game so that Bart can watch "The Krusty the Clown Show" (which has been running for 346 consecutive hours), Bart transports him into the football stadium in place of the ball for a field goal kick. As Homer creeps back into the house, trying to surprise Bart with a blow to the back of the head, Bart transforms him into a jack-in-the-box. Marge suggests that the two see Dr. Marvin Monroe. The doctor says that Bart is desperate for paternal attention from Homer and suggests that the two spend more time together. Despite being a jack-in-the-box, Homer spends quality time with Bart, and they soon become a normal, loving family. Bart turns Homer back into a human and the two share a warm family moment, causing Bart to wake up screaming. Bart and Lisa wake Homer and Marge and beg them to let them sleep in their bed. As the two crawl into bed, Homer laments that he has to go to work in a few hours and dozes off, becoming the last one in the episode to have a nightmare.
In the third segment, Mr. Burns fires Homer for his laziness and incompetence. Looking for work, Homer answers a classified ad to become a grave digger. Meanwhile, Burns is nearing the completion of his giant robotic laborer, which he hopes will eventually replace weak-bodied human workers. The only remaining step is to implant a human brain into the machine's body. Searching a graveyard the following night, Burns mistakes Homer, snoring in an open grave, for a newly buried corpse and, despite signs that Homer may still be alive, removes Homer's brain with an ice cream scoop and places it in the robot. However, Robo-Homer is just as lazy and incompetent as he was as a human, using his x-ray vision to locate donuts. Burns declares the experiment a failure and, after restoring the brain to Homer's still-living body, kicks the robot, which topples over and crushes Burns. Afterward, he tells Smithers to get some surgical tools and ether.
Homer wakes up screaming after Bart bites him. While going to the bathroom, Homer finds Mr. Burns's head grafted on his shoulder, Burns revealing that due to the fact his own body was crushed, Burns's head was instead surgically placed onto Homer's neck. Homer, in a stage of denial repeatedly mumbles that it is all a dream, and Mr. Burns sarcastically reassures him, "Oh that's right, it's all a dream... or is it?" and laughs maniacally.
In a faux sneak preview for the next episode, Lisa reminds Homer that her class is hosting an "all you can eat" spaghetti dinner while Burns reminds him that they have a reception for Queen Beatrix of the Netherlands that same night. This causes Homer to remark how he hates having two heads.