This is not in any way relevant to me, I'm just interested. What would happen if you chose a PhD thesis to work on, and halfway through someone somewhere in the world who was also doing a PhD in the same field published their thesis, which just happened to be the same research that you were doing. Would your research still be considered since it was done independently? Does this happen often? Are there safeguards to prevent this from happening, like would you normally search the academic field to see if anyone else is working on the same topic? Surely that would be almost impossible to do?
A lot of it depends on how far along you are, your institution, your career goals, and to some extent whether its your fault or not. That is, you DEFINITELY need to search the literature to see if your topic was already studied. But sometimes, someone might just have the same idea because they are thinking about the same issues and reading the same existing literature as you are.
If someone scoops you, it's likely a lot of your work won't be publishable anymore. In some fields it might be possible to publish as a replication (with low impact) or it may be that if you look carefully there is some areas of non-overlap that you can run with. The issue of publication will matter a lot more if you want a career in academia: you need a strong publication record to move on to the next phase of your career. In that circumstance, it might actually be better to delay your PhD just to get some publications, unless you expect you can get a good post doc regardless.
If you are early in your work, even halfway, it may be possible to switch topics. It's also possible all you need is a minor shift. I got scooped. Twice. Both times it hurt, but both times I also realized that there wasn't anywhere near 100% overlap, and my work was still publishable, it just took some extra time to rework things, reemphasize novelty, etc.
Overall, though, your PhD should be awarded for the work you do, not necessarily whether the work pans out. Your institution shouldn't prevent you from getting a PhD if you have been scooped. Negotiating all these issues should be the role of your thesis committee. The thesis committee should be comprised of at least some relatively unbiased professors who can judge whether or not your work is acceptable. The point of the PhD should be to train to be a researcher. Sometimes being scooped is part of the learning experience.
(note: I am answering this mostly from the perspective of biological sciences in the US - of course there may be differences by field and location)
Just start comparing to their works in your papers, explain clearly what you are doing differently and why the differences are relevant. Very rarely exactly everything will be the same, as long as it isn't - start focusing on describing the differences or additions that your work includes compared to the other works and stressing that your work was done simultaneously and independently.
Also practice in having in your head (but no where else!) several independently developed rephrasings / refocusings of your work can be a good thing. Kind of the same idea as to never show all your cards at once in a card game but have jokers up your sleeves / back up strategies et.c.
Here is a perspective from (pure) mathematics.
It is not that unusual in mathematics that a problem is solved (almost) simultaneously by two different people, one of whom will sometimes be a PhD student. Generally, both papers end up being published, and are treated as independent discovery; later on the student can include it in their thesis. This makes sense, especially since the ideas involved in both works will usually be different so both are useful to the community. (Publishing new proofs of old results is also not unheard of, but usually carries much less prestige, presumably a bit like replication studies elsewhere.) In borderline cases, the students tend to be treated slightly more leniently than established researchers, since it is understood that publications matter more to them (it may or may not help they have a supervisor who is often a well-respected researcher).
It should also be pointed out that PhD students will generally not embark upon projects which take many years to complete, so the window for simultaneous discovery is much shorter. Even if one tries to attack a big problem which takes a lot of time and effort, if any progress is being made then interesting results will appear in the process and hopefully be published (if this is not the case, it would be doubtful if the big theorem is likely to be proved at all).