Stanley Yelnats IV is an overweight teenage boy from a family of destitutes that is affected by "a hex", which they point the finger at Stanley's "no-good-dirty-rotten pig-stealing-great-great-grandfather". Stanley's latest adversity is to be wrongly accused of stealing a pair of shoes (after one bad day in school) contributed to a children's orphanage by the baseball player Clyde "Sweet Feet" Livingston.
As retribution, Stanley is sent to Camp Green Lake, a juvenile imprisonment and disciplinary facility which, not withstanding its name, is in the middle of a sterile desert. When he arrives at the ravaging place, Mr. Sir, the superintendent, apprises him not to cross Warden Walker. After an apprehensive start, he is slowly accepted into the company of the inmates in Tent D. He also befriends a child called Zero, who is disregarded as stupid by the other inmates, and by Mr. Pendanski, another superintendent. Zero and Stanley escape and have to get to the top of the mountain to pull through.
Stanley's great-great-grandfather, Elya Yelnats, is in love with a girl named Myra Menke, but a much older man named Igor Barkov is also trying to marry her as his wife. Igor is a pig farmer, and he offers his heaviest pig in exchange for Myra's hand-in marriage.
Desperate to impress Myra and her father, Elya goes to his friend Madame Zeroni, for help. She warns him that Myra is not intelligent and will not be a good wife, and advises him to move to America, as her son has. She gives him a tiny piglet, telling him to carry the piglet up a mountain every day, and let it drink from a stream and sing to it. Each day the water will make it grow a little bigger, and Elya will grow a little stronger. On the last day, after he carries the pig one last time, he must carry Madame Zeroni herself up the mountain to do the same, as he will then be strong enough to carry her.
Elya follows her directions, and the piglet grows to a large size, but he does not carry the pig up on the final day. Elya nearly wins Myra as his bride, but his pig is revealed to be the same weight as Igor's, because the pig did not drink the water that day. Elya realizes Madame Zeroni was right about Myra's stupidity and walks away in disgust. Forgetting to carry Madame Zeroni up the mountain, he moves to America to start a new life, falls in love, and marries, but he is beset by Madame Zeroni's curse. Stanley's friend Zero is revealed to be Hector Zeroni, Madame Zeroni's great-great-great grandson.
Normally in the book, Stanley sings a song in his mind that his father used to sing to him:
"If only, if only," the woodpecker sighs, "The bark on the tree was as soft as the skies." While the wolf waits below, hungry and lonely, He cries to the moo—oo—oon, "If only, if only."
At the end of the book, Hector's mother sings the full version of the song. Here is the second verse of the song:
"If only, if only, the moon speaks no reply; Reflecting the sun and all that's gone by. Be strong my weary wolf, turn around boldly. Fly high, my baby bird, My angel, my only."
The town of Green Lake was a flourishing lakeside community in the old west. Katherine "Kate" Barlow, the local teacher, has a romantic interest in Sam, an African-American onion seller. There is an uproar in the town after they are seen kissing in public. A mob led by a wealthy resident Charles "Trout" Walker assembles and Kate realizes Sam is in danger of being lynched. She finds Sam and they try to escape onto the lake in his boat, but Walker and a group of vigilantes catch up with them in another boat. Sam is shot and killed, while Kate is rescued against her wishes. Sam's murder curses Green Lake: no rain falls on the area, and eventually the lake dries up and the town is abandoned.
For the next 20 years, Kate is "Kissin' Kate" Barlow, a feared Texan outlaw who leaves a prominent kiss-mark in lipstick on her dead victims. Kate robs Stanley's great-grandfather, Stanley Yelnats I, of his entire fortune, but rather than kill him, she abandons him in the desert that was Green Lake. Miraculously, he survives.
Years later, Kate returns to an old cabin on the former lakeside and is tracked down by Charles Walker and his wife, who are bankrupt and desperate for money. They try to force her to reveal where she buried her loot, but she is bitten by a yellow-spotted lizard and dies. As she dies, she taunts them by saying, "You, your children and your children's children will dig for 100 years and you will never find it," and to "start digging". The Walkers are left to dig the entire area in order to find the buried suitcase. The Warden is the granddaughter of the Walkers, and is using the campers to search for the treasure.
Camp Green Lake
The inmates at Camp Green Lake are forced to dig cylindrical holes five feet deep and five feet wide, which the warden says "builds their character." They are promised a day off if they find anything interesting. During one dig, Stanley finds one of Barlow's lipstick tubes, but he pawns it off to X-Ray, the ringleader of Tent D. The Warden is excited by the discovery and orders them to greatly enlarge X-Ray's hole (the wrong hole) for several weeks. Stanley suspects that the Warden is looking for something.
Meanwhile, Stanley and Zero, the smallest of Tent D who got his nickname because "he has nothing in his head", become friends. Stanley agrees to teach Zero how to read, and in return, Zero assists Stanley in digging his hole. The other boys get jealous after seeing Stanley getting help, eventually resulting in a fight. The Warden and the rest of the staff arrive and learn that Zero has been assisting Stanley with digging. The argument culminates in Zero hitting Mr. Pendanski with a shovel and running away. Zero runs away, and the camp staff decide to erase their records of him and let him die in the desert. A few days later, Stanley follows Zero and finds him living under the remains of Sam's boat, eating very old jars of Kate's spiced peaches, which he calls "Sploosh". Stanley notices a mountain resembling a human fist giving the thumbs up sign, and recalls that Stanley Yelnats I claimed to find "refuge on God’s thumb". On the mountain, Zero admits that he was the one who stole "Sweet Feet's" shoes.
Atop the mountain, Stanley discovers a field of onions that was once Sam's. The boys eat the onions and find water by digging in the ground, and Stanley sings Madame Zeroni's song to Zero, unknowingly breaking the curse. Stanley suggests that they return to the hole where Stanley found the lipstick to find the buried treasure. They find a suitcase buried in the hole, but they are captured by the Warden, and surrounded by a group of lethal yellow-spotted lizards. A stalemate develops: they cannot move, but the lizards prevent the Warden's staff from approaching them. The lizards do not bite Stanley and Zero because they are repelled by onions (as Sam once said). They remain in the hole until the next morning, when an attorney arrives demanding Stanley's release, having received testimony that gives him an alibi during the time the shoes were stolen. The Warden tries to claim they stole the suitcase from her, but Zero reveals that the name 'Stanley Yelnats' is written on it, as the suitcase had belonged to Stanley's great-grandfather (Stanley Yelnats I), the one that had been robbed. After the camp staff are unable to produce Zero's file, he too is released.
Stanley's family open the case, discovering the jewels, deeds, stocks and promissory notes stolen from Stanley Yelnats I. Using the money raised from the bonds, Stanley's family buys a new house and Zero hires a team of investigators to find his missing mother; meanwhile, the drought at Green Lake is brought to an end by rainfall. The family's luck seems to change as if in response to Stanley's fulfillment of his ancestor's promise (a suggestion left purposely ambiguous by the narration). In a final scene, Clyde Livingston and his wife, along with the Yelnats and Zeroni families, celebrate the success of Stanley's father's antidote to foot odor, composed of preserved and fermented spiced peaches and onions and named "Sploosh" by Zero. The Warden is forced to sell Green Lake to "a national organization dedicated to the well-being of young girls", which turns it into a Girl Scout camp.