Gollum's evil side resurfaces with a vengeance as he leads Sam and Frodo into a trap--the lair of the giant spider Shelob. The smell in the place is horrible, but neither Sam nor Frodo realize they are being led into a trap. Gollum thinks Shelob will only care about prey, not the Ring he hopes to recover. Once inside the cave, Gollum disappears.
It is then that Sam and Frodo sense the evil presence of Shelob. It is dark in the cave so they cannot see her. Sam asks Frodo to use Galadriel's phial to cast light in the cave. They see the hungry evil eyes of their predator. As they try to run she spins a web to trap them. Shelob was, as Sauron called her, "his cat", whom he fed prisoners for which he had no use.
Sam and Frodo escape and come to another pass, where upon Sam hides the Phial, afraid that orcs might spy them. But as soon as he does that Shelob rushes forward from another hole. Just as Sam is about to warn Frodo, Gollum grabs him from behind. In the ensuing struggle, Sam and Frodo are separated. By the time Sam gets free, he cannot see Frodo.
It is unfortunate but necessary to the plot that Gollum has this last bout of evil intent. Shelob is merely another villain thrown in to complicate the plot and raise the anxiety levels of the characters and the reader.
Sam returns to find Frodo entrapped in Shelob's web, apparently dead or severely wounded. Enraged, Sam uses Frodo's sword to fight Shelob. Shelob fights him, but he miraculously recites an elven prayer and uses the phial; a burned Shelob crawls away. Sam sees that Frodo is lifeless and gives up in exhaustion and sadness.
After some time, Sam resolves to finish Frodo's task of destroying the Ring. He takes the Ring from his companion and leaves him in his coat of fine mail armor. He goes some distance before he hears the sounds of the orcs. He slips on the Ring just in time. His senses seem suddenly sharper, except for his eyesight. And he is aware of an eye searching for him. He hears the orcs and realizes that they have found Frodo, at which point he can only think of rescuing his friend's body from the enemy.
He rushes to where Frodo lay, but the Orcs have carried his body away. Sam realizes he can hear the orcs and understand their language. Eavesdropping, he learns that the orcs are looking for him; and further, he learns that Frodo is not dead, merely paralyzed by Shelob's venom.
Sam dons the Ring, an act that is full of danger and the threat of corruption. But Sam is a good person, as already established. He dons the Ring fully aware of its dangerous potential. He does so to honor his fallen friend, Frodo.
A convenience here is that Sam learns Frodo is not dead. Otherwise, he might have gone on without Frodo. As it is, he learns that his friend is still able to be saved.