What happens if someone publishes a paper on your current PhD thesis topic
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)
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).
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.