The key point here is that Sauron or Ringwraiths can always detect Frodo when he puts on the Ring -- evil calls to evil, and Sauron & the Ringwraiths are intimately tied to the Ring. But they can't do it instantly. This gives him time to react. So what happens on Amon Hen after he escapes Boromir?
In the book, Sauron very nearly does detect Frodo. Frodo puts on the ring, escapes Boromir, and goes to the top of a hill called Amon Hen. He sees visions of warfare and of Barad-Dur. Then...
And suddenly he felt the Eye. There was an eye in the Dark Tower that did not sleep. He knew that it had become aware of his gaze. A fierce eager will was there. It leaped towards him; almost like a finger he felt it, searching for him. Very soon it would nail him down, know just exactly where he was. Amon Lhaw it touched. It glanced upon Tol Brandir -- he threw himself from the seat, crouching, covering his head with his grey hood..
He heard himself crying out: Never, never! Or was it: Verily I come, I come to you? He could not tell. Then as a flash from some other point of power there came to his mind another thought: Take it off! Take it off! Fool, take it off! Take off the Ring!
The two powers strove in him. For a moment, perfectly balanced between their piercing points, he writhed, tormented. Suddenly he was aware of himself again. Frodo, neither the Voice nor the Eye: free to choose, and with one remaining instant in which to do so.
So what happened is that putting on the Ring made him vulnerable to Sauron finding him, but not instantly. Per above, that's the key point, here, and in Bree and elsewhere. He has some amount of time to come to his senses.
In this case, atop Amon Hen, turns out the Voice -- Gandalf! -- is basically magically fighting Sauron to give Frodo enough time to snap out of it and take the Ring off.
How do we know it's Gandalf? Because later, when he meets up with Aragorn, Gandalf tells Aragorn about it:
Very nearly it was revealed to the Enemy, but it escaped. I had some part in that: for I sat in a high place, and I strove with the Dark Tower; and the Shadow passed. Then I was weary, very weary; and I walked long in dark thought.
I think that in the movie they elided this Amon Hen episode a bit, so it's not as clear.