Sam sees that Shelob is already wrapping Frodo with webbing, so he leaps up with Sting to charge Shelob.
He stabs into the clusters of her eyes. Way to go Sam.
She tries to crush him under her, but he keeps right on stabbing, and to great effect. Shelob has never known such pain in all her long life.
Clearly she has never known Sam.
After jumping backwards out of the way, Shelob crouches, preparing for the final leap to take Sam out.
But Sam holds up the Phial of Galadriel, and suddenly, words come to him—a song in Elvish.
(This song in Elvish is a variation of one that appears in The Fellowship of the Ring 2.1.157, chanted by the elves of Rivendell in the Hall of Fire. It is a hymn to Elbereth, the Queen of the Stars. Check out this translation.)
After speaking this hymn in a language he does not know, he shouts, "Come on, and taste it again!" (4.10.10).
His determination makes the jewel of Galadriel shine more brightly than ever, and the sheer power of it sends Shelob crawling in agony back into her holes.
A victorious Sam crouches next to Frodo.
Shelob has stung Frodo right in the back of the neck, and now he is cold all over.
Things don't look good. Sam tries to call him back, but Frodo does not respond, and Sam is sure that Frodo is gone.
Needless to say, the poor guy is devastated.
Eventually, Sam pulls himself together and decides that he has to continue on and finish the quest. If he doesn't, who will?
(He would also like to kill Gollum, if he can, but that's his second priority.)
Sam will leave Frodo his things (except for Galadriel's Phial, the Ring, and Sting, since he'll need those), and he promises to come back to Frodo when he's finished.
Poor Sam is utterly miserable, though: he can't imagine how he is to continue on to the Cracks of Doom by himself.
Still, he knows that there is no other choice. Sure, he doesn't want to go, but neither did Frodo.
So Sam gently takes the Ring's chain from around Frodo's neck and puts it around his own.
With one last look at Frodo's pale face, he stumbles off.
No sooner has he reached the dark orc-tower at the far end of Cirith Ungol, than he hears orc voices coming up behind him.
Time to hide. Sam puts the Ring on to slip out of sight.
The Ring enhances Sam's hearing, so that he can overhear the orcs' conversation clearly.
Apparently, they have found Frodo's body lying in the tunnel.
So Sam immediately decides to rush back to Frodo's side.
He chases after the orcs towards the Undergate, but he quickly falls behind because he is, plainly, pooped.
He stands eavesdropping outside a low stone gate that keeps him out of the tunnels.
The orcs all sound terrified of Shelob. They don't want her to come and eat them.
But they are pleased to find Frodo, since he is something "Lugbúrz" (4.10.65)—a.k.a. Barad-dûr—wants. Of course they don't actually know what he is, but they think he looks Elvish.
Sam is certain that they are stealing Frodo's body "for some foul purpose" (4.10.72).
Then he overhears Shagrat telling Gorbag that there is news of spies on the stairs (well, they got something right at least).
That's why they are on high alert.
The orcs call Gollum Shelob's "Sneak." He's a "thin little black fellow, like a spider himself" (4.10.88). They say he came out of Barad-dûr the first time, but since then he's come up to Cirith Ungol several times.
He's got some kind of understanding with Shelob, Shagrat comments.
As they examine Shelob's webs, they realize that Frodo couldn't have been alone.
Gorbag thinks it's probably a large Elf warrior, but Shagrat thinks he's being too pessimistic. Little do they know that Sam—the large Elf warrior—is nearby.
Anyway, they are here to take a prisoner and that's just what they do.
After all, Shagrat has orders: "Any trespasser found by the guard is to be held at the tower. Prisoner is to be stripped. Full description of every article, garment, weapon, letter, ring, or trinket is to be sent to Lugbúrz at once, and to Lugbúrz only. And the prisoner is to be kept safe and intact" (4.10.102).
That's when Sam figures out that Frodo is actually still alive. Apparently, Shelob's poison isn't fatal because she likes her victims living as she eats them. Gross.
Sam almost faints as he realizes he's left Frodo alone, alive, to be taken by orcs. It's good news, sure, but he's totally ashamed that he left his buddy behind.
He thinks that he is trapped on the other side of the stone gate from Frodo, but he sees that the gate is actually low enough for him to climb over.
It was probably just designed to keep Shelob in the tunnel, rather than to keep people out of it.
Sam hears Shagrat telling Gorbag they will put Frodo in the top chamber of the tower because he wants Frodo to be safe from the other orcs, who may lose their heads and damage him.
There is a loud blaring of horns, and the gate to the orc tower swings closed.
Sam is shut on the wrong side once again.
But hey, at least Frodo is alive. And captured…
To find out what happens next, you'll just have to read The Return of the King.