Generally speaking, a good Ph.D. topic will be interesting to you, your advisor, and the research community. If you pick a topic you're not passionate about simply because it is your advisor's pet area or it's a marketable subject, it will be difficult to stay focused and motivated. You'll have a hard time finishing and a harder time convincing other people that your topic is interesting.
Read up on what's going on in your general field of interest, and familiarize yourself with the other workers in the area. Introduce yourself to authors at conferences or through e-mail, and tell them about your work. By starting a dialogue, you will usually find what is being worked on and what branches are still untouched. You'll need to find a particular problem that is related to work being done (so that you'll have other people and papers as resources and an interested community) but is different in an important way (so that you can contribute original work).
Choosing a thesis problem is a gradual process, not usually a discrete event; don't feel like you have to sit down and immediately think of one. If you've been working in a field for a little while, you'll eventually be able (with the help of your advisor or others) to find a central problem that's tractable in a few years and that will have a definite solution. Really good problems will have solid answers, but they will branch off into more speculative areas as well. Your thesis will then be worthwhile even if these branches don't pan out into useful results or prove too difficult to include in only a few years. Remember, research careers span many more years than dissertations do, so don't get caught in the trap of doing "just one more thing" before completing your thesis. Part of your research should be to define future problems and to leave them for the future.
Topics can be placed in a spectrum from "flaky" to "cut-and-dried." Flakier theses open up new territory or explore previously unresearched phenomena but have an increased risk of being too speculative, ungrounded, or unrelated to the current community. Cut-and-dried theses rigorously solve well-characterized problems but are not usually splashy or high-profile products. Both types are valuable; where you situate yourself in this spectrum is a matter of personal style--how much risk you are willing to assume and how much you are willing to immerse yourself in work.
Whatever you do, it has to be something that has not been done before. Don't try to work on something that someone else is doing simultaneously and hope that you'll finish first. There's enough unexplored science out there that there's no need for competition. However, if you come across a paper that appears to solve your thesis problem, DON'T PANIC. Take a closer look; the resemblance is often only superficial. If it's more, show the paper to some wise person who knows your work and ask them what they think.
Once you identify a thesis topic, be sure you can summarize it in one-sentence, one-paragraph, and five-minute bits. These will be useful to keep yourself focused and will also help others take you seriously when you talk about your work. When doing the actual work, be sure you are able to explain how each step contributes to the goal. Define a discrete end-point with the help of your advisor so that you don't have different expectations. Plan on spending about a year writing the actual paper because you will invariably run into unfinished problems or mistakes along the line. The total time to completion is often described as "a year beyond when you're utterly sick of the topic." When this happens to you, try to break down the thesis into manageable tasks, and set deadlines for each part. Set aside time to be away from your work, to exercise or participate in a club, or just to do something so that you won't be tempted to spend hours fiddling aimlessly. Great examples of "thesis avoidance" are well known; talk to other students about strategies to stay focused.