The following things are true to a greater or lesser extent at various points in the timeline while Gollum is traveling with Frodo and Sam.
- Gollum takes his oath on the ring seriously, and believes in the ring’s power to punish him for breaking it.
- Gollum is conflicted and part of the non-corrupted Sméagol has come back to the surface and that part doesn’t want to kill or even have the ring.
- Gollum knows that Sam doesn’t trust him and he doesn’t trust Sam. He can’t be 100% sure that Sam isn’t faking being asleep.
- Gollum knows it’s a big risk to try to take the ring himself. If he accidentally wakes either of them, he probably will be killed.
- Gollum has a plan. He remembers Shelob and knows that she is there and how to navigate her tunnels. His plan is to lead Frodo and Sam to Shelob and hope to find the ring among the clothes, etc., that Shelob casts away.
Not part of the answer but I expect the downvotes could be related to all of these things being made pretty clear in the books.
Less clear in the books but definitely alluded to are the facts that the ring has a will, and that there is some other will at work also. The ring is sticking with Frodo, or at least not betraying him. If Gollum got the ring back, he might hide again somewhere dark and deep for further centuries. The ring has been down that road and won’t likely make its way back to its master by betraying Frodo in favor of Gollum.
The other will is more mysterious, but seems to be working toward the ring’s destruction. Is it just luck that it was Bilbo who found the ring, and not an orc of the Misty Mountains, when the ring betrayed Gollum? Was it only Bilbo’s and Gandalf’s strength of will that made it possible for Bilbo to give it to Frodo? As much as the ring is trying to find its master, there seems to be something working against the ring and Sauron.
Neither of these wills would be helped by Gollum getting the ring, so if either or both of those forces exist, they would also be working to prevent that.
This passage (from The Two Towers, chapter 2, “The Passage of the Marshes”) alone seems enough to answer the question (this argument is between the two sides of Gollum/Sméagol):
‘Sméagol promised,’ said the first thought.
‘Yes, yes, my precious,’ came the answer, ‘we promised: to save our Precious, not to let Him have it – never. But it’s going to Him, yes, nearer every step. What’s the hobbit going to do with it, we wonders, yes we wonders.’
‘I don’t know. I can’t help it. Master’s got it. Sméagol promised to help the master.’
‘Yes, yes, to help the master: the master of the Precious. But if we was master, then we could help ourselfs, yes, and still keep promises.’
‘But Sméagol said he would be very very good. Nice hobbit! He took cruel rope off Sméagol’s leg. He speaks nicely to me.’
‘Very very good, eh, my precious? Let’s be good, good as fish, sweet one, but to ourselfs. Not hurt the nice hobbit, of course, no, no.’
‘But the Precious holds the promise,’ the voice of Sméagol objected.
‘Then take it,’ said the other, ‘and let’s hold it ourselfs! Then we shall be master, gollum! Make the other hobbit, the nasty suspicious hobbit, make him crawl, yes, gollum!’
‘But not the nice hobbit?’
‘Oh no, not if it doesn’t please us. Still he’s a Baggins, my precious, yes, a Baggins. A Baggins stole it. He found it and he said nothing, nothing. We hates Bagginses.’
‘No, not this Baggins.’
‘Yes, every Baggins. All peoples that keep the Precious. We must have it!’
‘But He’ll see, He’ll know. He’ll take it from us!’
“He sees. He knows. He heard us make silly promises – against His orders, yes. Must take it. The Wraiths are searching. Must take it.’
‘Not for Him!’
‘No, sweet one. See, my precious: if we has it, then we can escape, even from Him, eh? Perhaps we grows very strong, stronger than Wraiths. Lord Sméagol? Gollum the Great? The Gollum! Eat fish every day, three times a day, fresh from the sea. Most Precious Gollum! Must have it. We wants it, we wants it, we wants it!’
‘But there’s two of them. They’ll wake too quick and kill us,’ whined Sméagol in a last effort. ‘Not now. Not yet.’
‘We wants it! But’ – and here there was a long pause, as if a new thought had wakened. ‘Not yet, eh? Perhaps not. She might help. She might, yes.’
‘No, no! Not that way!’ wailed Sméagol.
‘Yes! We wants it! We wants it!’