As others have indicated, the only 100% effective method of preventing getting "scooped" or finding out that your result already exists in the literature is that of complete abstinence: i.e., not trying to do any research at all.
Obviously this method is far too draconian for most of us on this site. I want to support statements of Gowers and Nielsen: finding out that what you have just proven has already been proven by someone else is far from the worst thing in the world. (Finding out that what you've proven, or published, is false, is much much worse, for instance.) On the contrary, for a mathematician who is making her own way and working on problems of interest to her, if you are doing any good work at all it is inevitable that you will duplicate some past research. This can be very encouraging: when I was younger, I often lacked confidence that some things which were of interest to me were of sufficient interest to everyone anyone else (all I knew at that point was what people in my department near to me were workingon)doing).
I remember in particular that as a first year graduate student, I discovered that every profinite group is, up to topological isomorphism of topological groups, the automorphism group of some Galois extension. This seemed neat but I thought, "Well, if anyone really cared, I would have heard about it before." Wrong -- this result has been published several times; off the top of my head by Leptin and by Waterhouse, but I know this list is not complete -- and in some texts (just not the ones I knew about at the time) it appeared appears with due respect and appreciation. When I found out that someone had written and published a paper containing exactly the same mathematics that I had done, it was very encouraging.