The most interesting and most important thing about Lord of the Rings is that Frodo fails the quest. He fails the quest, he can’t destroy the ring, he claims the ring for himself, only for it to be taken and accidentally destroyed by Gollum. There are so many ways to interpret that moment!
You can say: evil always defeats itself. The ring destroyed Smeagol, and Gollum destroyed the ring.
You can say: it’s divine providence. Fallible mortals cannot attain grace by their own power. No matter how they try, they will fail unless providence lifts them up. But providence will lift them up. Very Romans 9:16. It depends not upon man’s will or exertion, but upon God’s mercy.
You can say: actions have unpredictable consequences, and here, a series of unrewarded acts of compassion finally gained their reward. There is such a thing as fate, and it is, in its own way, fair. Multiple characters had the chance to kill Gollum, and a good reason to do it too, since he tried to kill them first, and was still a threat. But Bilbo chose to spare him, Frodo chose to spare him, (during the Council of Elrond it’s briefly mentioned that Aragorn and Gandalf and a serious number of elves chose to spare him), and a few minutes before he attacked Frodo, Sam very reluctantly chose to spare him for a final time. If any of them had chosen to kill Gollum, even in reasonable self-defence, he could not have played his part in destroying the ring. But in the decisive moment, the world showed compassion to Frodo as a reflection of the compassion he had shown.
You can say what LeGuin said: Frodo and Gollum are essentially two halves of the same person. Of course the hero can only complete his goal after a violent struggle with himself, only it’s the good half of him that fails, and the evil half that in the end achieves the quest.
You can say: the last time Frodo and Gollum met, Frodo forbade him from ever laying a hand on him, and cursed him to fall into the flames if he tries. The power of the ring, or the power of Frodo wearing the ring, actually caused this to come true. (See this post about Frodo laying a geas.)
What I myself feel and say is this: Gollum thought he was attacking Frodo and taking the ring for himself. What he was actually doing is saving Frodo and saving the world. He saved Frodo from the Ring, from Sauron, from the failure of his quest, from something far worse than mere death. And he’s the only one who could do it: Sam had carried Frodo, he had fought Shelob for Frodo and would gladly die for Frodo, but he could never have hurt him, or taken the ring by force. Gollum could, but only because he didn’t know why he was doing it. He was a sleeper agent of Good. He thought he was attacking Frodo and grabbing the ring for himself, when in fact he was giving his own life, to save his Master whom he loved.