Ali Baba, a poor Arab woodcutter, happens to overhear a group of thieves—forty in all—visiting their treasure store in the forest where he is cutting wood. The thieves' treasure is in a cave, the mouth of which is sealed by magic—it opens on the words "Open, O' Simsim" (commonly written as "Open Sesame" in English), and seals itself on the words "Close, Simsim" ("Close Sesame"). When the thieves are gone, Ali Baba enters the cave himself, and takes some of the treasure home.
Ali Baba borrows his sister-in-law's scales to weigh his new wealth of gold coins. Unbeknownst to Ali, his brother's wife has put a blob of wax in the scales to find out what Ali is using them for. To her shock, she finds a gold coin sticking to the scales and tells her husband, Ali Baba's rich and greedy brother, Cassim. Ali Baba tells Cassim about the cave. Cassim goes to the cave to take more of the treasure, but in his greed and excitement over the treasures forgets the magic words to get back out of the cave. The thieves find him there, and kill him. When his brother does not come back, Ali Baba goes to the cave to look for him, and finds the body, cut in to many pieces. Ali Baba bring's the body home and, with the help of Morgiana, a clever slave-girl in Cassim's household, he finds a blind tailor and leads him to Cassim's house. There, overnight, the tailor stiches Cassim back together, so that his wife and neighbours will not be suspicious. Ali and his family are able to give Cassim a proper burial without anyone asking awkward questions.
The thieves, finding the body gone, realize that somebody else must know their secret, and set out to track him down. One of the thieves goes down to the town and asks around. He discovers that a tailor was seen leaving a house in the early morning, and guesses that the house must belong to the thieves' victim. The thief finds the tailor and asks him to lead the way to the house. The blind tailor is able to do this and the thief marks the door with a white cross. The plan is for the other thieves to come back that night and kill everyone in the house. However, the Thief has been seen by Morgiana and she, loyal to her master, foils his plan by marking all the houses in the neighbourhood with a similar cross. When the 40 thieves return that night, they cannot identify the correct house and the Head Thief kills the unfortunate man. The next day, the Thieves try again, only this time, a chunk is chipped out of the stone step at Ali Baba's front door. Again Morgiana foils the plan by making similar chips in all the other doorsteps. The second thief is killed for his stupidity as well but eventually they are able to ascertain the location of Ali Baba's house.
The lead thief pretends to be an oil merchant in need of Ali Baba's hospitality, bringing with him mules loaded with thirty-eight oil jars, one filled with oil, the other thirty-seven with the other thieves (the two missing members were the scouts previously sent to find the house, who were killed for their failure). Once Ali Baba is asleep, the thieves plan to kill him. Again, Morgiana discovers and foils the plan, killing the thirty-seven thieves in their oil jars by pouring boiling oil on them. When their leader comes to rouse his men, he discovers that they are dead, and escapes.
To exact revenge, after some time the lead thief establishes himself as a merchant, befriends Ali Baba's son (who is now in charge of the late Cassim's business), and is invited to dinner at Ali Baba's house. The thief is recognised by Morgiana, who performs a dance with a dagger for the diners and plunges it into the heart of the thief when he is off his guard. Ali Baba is at first angry with Morgiana, but when he finds out the thief tried to kill him, he gave Morgiana her freedom and she marries his son. Thus, the story ends happily for everyone except the forty thieves and Cassim.