Sometime before Puss (Antonio Banderas) meets Shrek and Donkey in Shrek 2, he arrives at a town while escaping a bounty hunter. There he learns that the outlaw couple Jack (Billy Bob Thornton) and Jill (Amy Sedaris) have the magic beans he's been looking for most of his life, beans that can lead him to a giant's castle holding valuable golden goose eggs. When Puss tries to steal them from the outlaws' room, a masked cat (Salma Hayek) interrupts. Both fail and escape, and Puss follows the cat back to Cat Cantina, a club, where they have a dance-off and a sword fight, ending when Puss hits the masked cat in the head with a guitar. He learns that the masked cat is Kitty Softpaws, and is shocked to learn she is a girl. She is allied with Humpty Alexander Dumpty (Zach Galifianakis), a talking egg and Puss' long-estranged childhood friend from the orphanage where he was raised. Puss tells Kitty of his feelings of betrayal for a youthful misadventure when Humpty tricked Puss into helping commit a crime. It turned out that Kitty was sleeping through the story. Humpty attempts to convince Puss to join them in finding the beans and retrieving the golden eggs, which he does.
The trio steals the beans from Jack and Jill and elude the angry outlaws in a canyon chase. As Humpty leads his compatriots to the spot where they must plant the beans, Puss and Kitty's relationship begins to grow from rivalry into friendship. The trio ride the fast-growing beanstalk into the clouds where, Humpty explains, they'll find castle of the late giant. Now, he continues, a fearsome monster called the Terror guards the Golden Goose. When they realize the golden eggs are too heavy to carry, they steal the Goose which is just a gosling and escape the castle and the Terror. While celebrating their victory, the group is ambushed by Jack and Jill, who knock Puss unconscious.
When Puss wakes up, he tracks Jack and Jill back to his old hometown, where he learns that the entire heist was a plot by Humpty to lure him home to be captured, as revenge for abandoning him to the authorities when Humpty's youthful heist went bad. Jack, Jill, and even Kitty were involved in the con. After pleas from his adoptive mother, the head of the orphanage, Puss turns himself in to the guards while Humpty donates many golden eggs to the town and becomes a hero.
While in prison, Puss meets the original Jack from the "Jack and the Beanstalk" (a.k.a. Andy Beanstalk) story who warns him that the Terror is in fact the Golden Goose's mother, and it will stop at nothing to get its child back. A repentant Kitty helps Puss break out of prison and tells him that she loves him more than gold. Puss convinces Humpty to help him fight off the Terror, saying he knows Humpty is a good person at heart, and he will be forgiven if he helps save the town. The Terror arrives, revealing itself to be a giant goose. Using the Golden Goose as bait, Puss and Humpty lure the Terror out of the town, but Humpty and the Goose are knocked off a bridge with Puss holding on to them. Humpty knows Puss cannot hold both of them, and he lets go, sacrificing himself to save the Goose and the town. Humpty's shell cracks open to reveal that he was a golden egg on the inside. The Terror then takes the Goose and Humpty away back to the giant's castle.
Puss is forced to flee because he is still an outlaw, but his efforts to save the town make him a hero among the townspeople. Puss and Kitty escape the guards once more, and Kitty says she will see him again soon, showing that she has taken his boots. In the epilogue, Jack and Jill are recovering from their injuries after being crushed by the Terror, Humpty is shown once again in his regular egg form, wearing a golden egg suit, as he rides the Terror into the clouds, and Puss and Kitty head back to dance at Kitty's hideout, the Glitter Box, where they finally kiss.