I like to listen to indie rock whenever I'm writing anything, other people I know think that classical or tribal (ugh.) music is the way to go.
What gets your mind going? Do you need total silence or are you incapable of coding without your favourite tunes?
However, when I get to a really difficult problem, I prefer total silence without any distractions. Again, earplugs are good for that. But if I need to, I'll go for a walk somewhere quiet to think about it (somewhere with a whiteboard, and/or maybe talk the problem over with a friend/colleague). Or I might even have to sleep on it. In all of these cases, there's no music involved. Music is more for the relatively easy/routine coding.