I’m only answering from the book. I’ll ignore the movies, which I also haven’t watched in their entirety. So, first, it seems that it takes a while to locate the ring, when it is used. On Amon Hen. then Frodo senses the eye trying to locate him, getting closer and closer, and he just removes the ring in time.
When Samwise puts it on, Sauron’s attention is gripped by the war he is fighting, on directing his armies and trying to capture the ring which he thinks Aragorn has. Also Sam has the phial of Galadriel which helps to mask it.
Seems that geography helps also - at the party when Bilbo puts it on, and later when Frodo puts it on at Brie, then they have the misty mountains and a long distance between them and Frodo.
When Sam puts it on also, he was just outside Mordor proper, with the mountains still in its way between him and Sauron. Then, it says
“He felt that if once he went beyond the crown of the pass, and took one step vertibaly down into the land of Mordor, that step would be irrevocable. He could never come back.Without any clear purpose he drew out the Ring and put it on again. Immediately he felt the great burden of its weight, and felt afresh, but now more strong and urgent than ever, the malice of the Eye of Mordoer, searching, trying to pierce the shadows that it had made for its own defense, but which now hindered it in its unquiet and doubt”. The Return of the King
So you have the unquiet and doubt of Sauron there, and you also have the shadows it made to defend itself. Plus Sam only puts the ring on for a short time and uses it mainly to find out what happened to Frodo. Then, later when he is tempted to use the ring, he is no longer wearing it, just contemplates putting it on again but doesn’t do so.
When Frodo and Tom Bombadil put it on in Tom Bombadil’s house, then the magic of Tom Bombadil masks it so the black riders can’t find it.
And earlier on, Gollum was deep underground, so that’s the geography of the large mountains above him masking it. And Sauron also was kind of asleep, only slowly taking form again.
So it’s a combination of