Zero is still alive, but Stanley realizes quickly that his friend is in bad shape. Zero refuses to go back to Camp Green Lake. He offers Stanley some "Sploosh" to drink - a dubious jarred concoction that Stanley notices tastes like peaches. Zero has been living off this jarred fruit, but there are no more jars.
Stanley shows Zero the mountain that looks like a thumb, and the boys set off across the desert again with God's Thumb as their destination. The going is tough, especially since Zero's health is failing and the boys have no water. They distract themselves with spelling games and speculating about what they'll find at the top of the mountain.
As they climb up toward the base of the thumb-like rock formation, the boys notice that there is more greenery and more bugs - sure signs of water nearby. Zero collapses, and Stanley carries him toward God's Thumb, eventually collapsing as well into a muddy ditch. Stanley joyfully gulps down the muddy water, and hands Zero half of a wild raw onion he finds growing nearby.
Zero confesses that he stole the shoes that Stanley was accused of stealing, and he is the reason why Stanley was sent to Camp Green Lake in the first place. Stanley, disbelieving, sings Zero his family's lullaby as the exhausted, weakened boy drifts off to sleep.
Stanley and Zero dig a deeper hole for water, and they discuss Zero's life. Zero explains how he stole Clyde Livingston's shoes from a charity auction at the homeless shelter Zero sometimes went to when he was homeless.
Stanley feels a deep sense of calm and happiness as he looks up at the stars that night, glad that the whole sequence of events happened to bring him and Zero together, and to allow them to survive. A crazy idea comes to him: what if he and Zero went back to Camp Green Lake and dug for buried treasure in the hole where Stanley originally found Kissin' Kate's lipstick tube?
The discovery of the onion field near God's Thumb prompts the narrator to recount a brief flashback in Chapter 40. This chapter reaches back to before Sam's death, and describes an everyday scene in Green Lake: a grateful townswoman thanks Sam for the onion remedies which helped cure her daughter. Although this anecdote does nothing to further the plot in itself, it serves as a testament to Sam's kind and honest character, and also reminds the reader that the past and the present are inextricably linked.
By including this flashback, Sachar makes it clear to us that the Yelnats owe their lives to this onion field - it is the same one which saved Stanley's great-grandfather when he was robbed. It also emphasizes the magical healing qualities of the onions, an important symbol in the novel.
The flashback is also important in that it is one of the reader's last images of Sam. Sachar chooses to leave us not with the sad and horrible scene of Sam's death, but with a scene that encapsulates the onion-picker's best qualities: his integrity, his friendliness, and his community spirit. It is a very different picture than our last image of Kate, or of Trout Walker, whom we leave in the barren desert, her heart broken and his ambition thwarted. Sam is such an unambiguously morally good character in Holes that it makes sense for Sachar to pay him - and the nicer inhabitants of Green Lake - tribute in this way.
There is the possibility of significant dramatic irony at the end of Chapter 39, if a reader has been paying close attention. It is fascinating that Sachar does not accompany arguably the most important event in the novel, one which alters the course of history and breaks a generations-old curse, with more fanfare. There is none of the lightning of Chapter 29, dramatically symbolizing Stanley's epiphany regarding God's Thumb. Sachar does not explicitly state - or even hint - that the curse is lifted. It is up to the savvy reader to recall the words of Madame Zeroni, and put two and two together: Stanley has now carried Zero up the mountain, and sung him the lullaby. If readers do not realize it at this point, they will have to wait until much later in the novel, because the time that the two boys spend on God's Thumb is a limbo period for them and for the action of the book.