Odysseus had a deep desire to see the Cyclops, which overrode his common sense.
When they saw all this, my men begged me to let them first steal some cheeses, and make off with them to the ship; they would then return, drive down the lambs and kids, put them on board and sail away with them. It would have been indeed better if we had done so but I would not listen to them, for I wanted to see the owner himself, in the hope that he might give me a present. When, however, we saw him my poor men found him ill to deal with. (Book IX)
Odysseus' pride and curiosity get the best of him in this situation. He wants to meet this lawless and primitive Cyclops, Polyphemus, and have the honor of claiming a guest-gift from him. The text doesn't explain, but it was probably something that Odysseus would want to boast about later. Also, Odysseus may have been trying to set a good example for his men. He would have preferred to take the food from the Cyclops as a gift, rather than steal it as his men suggested.
When the Cyclops returns he throws down his firewood with such a loud noise that Odysseus and all his men hide in a corner of the cave. While they are in the back of the cave, Polyphemus milks his goats and rolls a huge stone in front of the doorway of the cave, in order to keep his livestock inside. Since Odysseus and none of his men can move the stone, they are effectively trapped. The following day, the Cyclops keeps the men trapped in the cave by letting himself out and then quickly rolling the huge stone back over the doorway.
Odysseus gets Polyphemus very drunk, and he and his men drive a heated stake into his eye. This wakes Polyphemus, and he yells for the other Cyclops to help him, but Odysseus has told him that his name is Noman. Therefore, when Polyphemus yells to his friends, who have gathered outside the stone in front of the cave door, they think that he has been hurt by "no man" and therefore must be ill. They leave, and Polyphemus, in his pain and anger, rolls the stone away. Odysseus and his men escape by clinging to the bottom of sheep, so that the blinded Cyclops cannot feel them as they depart, only the wool backs of his own livestock.
Odysseus exhibits significant immaturity in this episode. If he had not shown such curiosity and bravura in the Cyclop's absence, he and his men could have gotten away easily.