Debugging
It is possible to write some code which runs perfectly the first time and does exactly what it is supposed to do. It happened to me once, and I was quite surprised. Without doubt, developers spend a good deal of time debugging and troubleshooting. This aspect of development can be extraordinarily frustrating, especially if you are under deadline or not very good at it. But successfully "shooting a bug" can also be quite satisfying.
Troubleshooting skill is partly technique, partly knowledge, and partly intuition born of experience. I suspect most developers would be hard-pressed to tell you how they do what they do. But you should get better at it the more often you do it.
General guidelines
Like a detective, troubleshooting a problem is first about gathering evidence or clues, and then it's about putting those clues together to point to the source of the problem. When presented with an issue, gather information from log messages, screen shots, output files if you've been a developer...