I’m just going to elaborate a bit on @tom_g ‘s third point.
You have to become acquainted with how your attention works. This requires developing a “meta-attention”: attention to your attention. That’s what all meditation boils down to, really.
Everyone has two modes of attention that work in tandem. There’s “open monitoring” (OM) mode, a global awareness of what’s going on; and “focused attention” (FA) mode, which is targeted at specific objects.
OM is constantly operating, largely subconsciously, scanning sensory experience and inner states for developments of interest or concern. When it picks up on something that it considers worth checking out more closely, it mobilizes the FA mode to zoom in on that target, highlighting it.
Unlike OM, FA works at a conscious level, so it’s the mode that can potentially be subjected to some control. While FA is directed at an object—a task, say—OM is still scanning the world for other potential objects of interest. It will keep proposing that the spotlight of FA move from the task at hand to check out this or that alternative object.
This is where meta-attention comes in. You can develop the ability to see when FA is being lured away, and you can learn to override that lure. Meditators typically learn to do this by anchoring the FA to something like the breath, then watching carefully as the OM keeps trying to redirect the FA elsewhere. Eventually, you learn that you can see those redirects coming, and simply turn them down.
This is the faculty psychocolgists call “executive control”, basically, the ability to consciously say “no” to impulses. Some people have developed this to a higher degree than others, but it does appear to be a faculty that remains somewhat plastic.
What Tom said about anxiety is true, too. When you feel threatened or under stress, OM’s scanning gets ramped up because it’s more feverishly looking for threats. It’s harder to keep FA still amidst that storm of redirect calls.