It's really a case of thinking about what you find interesting, and what will hold your attention for however long you have to write the piece of work. I personally think that's the most important thing. There will be ups and downs in this time, and it will be much easier to get through it if you care about the work.
Otherwise, the guidance is pretty standard:
Good luck. I'm sure you will receive some good advice here.
There are probably as many answers as there are students! I know people who knew what they wanted to write about even when they were undergrads, so they applied to a specific school with a specific advisor who could help them with that subject.
It may also depend on the school/program you're in. They might have rules about finding a topic and an advisor before you apply, or you might be able to figure things out along the way, like I did. For me, I had a general idea of the time period I wanted to study, but I didn't narrow down a topic until I was already in the PhD program. I took a class about medieval violence and vengeance, and my essay for that class turned into the nucleus of my thesis (and then turned back into a smaller article, which was my first publication). I've been asked about this in job interviews...how did I come to this field? Well it just sort of happened accidentally! Why do I study this? Because it's neat! Those explanations have never gotten me a job but I stick by them!
So there isn't necessarily one right way to do it, and sometimes it happens in unexpected ways.
My advice is to pick a topic strategically, the kind of thing you know markers will like. My thesis was on price discovery mechanics in the pre-war Tokyo stock exchange, which won me a prize for best thesis, not because it was extremely well-written (my friends are significantly better writers and researchers than I am), but because it ticked a lot of boxes which made it seem impressive.
Language: Theses which use sources in a foreign language are always impressive, be sure to include extracts of the source in your Appendix to show off.
Statistics: One of the biggest criticisms of history is that it's often wishy-washy and has no genuine grounding. This can be solved with the power of statistics, and I mean genuine statistical analysis with the appropriate tests and robustness checks and everything. Most historians don't actually understand statistical analysis, but they all really see the point of it and will be impressed by what they think is super complex but is actually relatively simple.
New Work: The best way to make your thesis stand out is to point to a bunch of other theses and say "to my knowledge there are no theses which do xxx" and then have some commentary on the shortcomings of not doing that.
Inter-disciplinary: This works very well if you have strong understanding of something very technical (e.g. engineering, finance, physics). You can then conduct very basic technical analysis, which will sound very impressive to a pure historian.
Algebra: One of the most basic types of mathematics, but historians will rarely come across it and will be shocked. This is distinct from the statistical analysis, you want to use this to express concepts. For example, a normal thesis will talk about the various advantages of the Mongol compound bow and maybe some case studies. Imagine how your marker would react to an equation for the force generation to draw weight ratio, with genuine understanding and analysis of the implications of course.
Data: Most historians will use case studies. Sometimes the entire thesis will just be about one case. This is fine and is an accepted way of doing things, but is extremely prone to omissions and biases. An impressive data source will be large ("we compiled 224,000 maintenance reports for tanks used on the Western Front"), exhaustive ("these represent all reports submitted during the war"), and embrace rather than skirt around biases ("the data is likely biased due to the most damaged tanks being scrapped rather than repaired")
Do all these and you will probably have a very impressive thesis.
The above posts are all terrific. I would also add this: be flexible. I had an idea of what I wanted to do, but then I was denied access to Egypt (I work on 19th century Egyptian history) and I had to do my research in Europe. The actual topic of the thesis was based on a discovery in the archives—an archive I hadn’t expected to be working in, and it went from there. I managed to write a thesis on 19th and early 20th century Egypt without actually doing any research there (god bless the Internet and friends at Egyptian universities!)
In my experience, most researchers change or drastically revise their topics based on what evidence they actually find—the only ones who haven’t had already done quite a bit of work and knew what they were likely to encounter—and many are quite panicked about it (“but I said I was going to do x!”). It’s all part of the process!