Well, there are a lot of articles on the internet about this topic. You can search for them, if you want some science behind that. If you want my personal opinion - it depends on the person, the type of music and what are your relationships with this music. Breaking it down into categories:
1. I'm the type of person that don't get distracted even by Cannibal Corpse, Death, Make Them Suffer, etc. (pretty much all hardcore and metal genres). So for me it's pretty much "always go with the music". You might be different and a single sound can harm your concentrating ability.
2. Type of music is more of a science. It's proven that classical music is able to increase your level of concentration and in some cases even cure some diseases. On the other hand, some part of your brain tries to follow up the rhythm and predict changes in patterns, etc. If you perfectly memorized tempo and patterns of Mozart (not perfectly, but you know what i mean, i hope) then yeah, it might help. On the other hand, again, it depends on personality and type of music.If you're doing routine homework and you don't enjoy it or have no motivation, your favorite music might help to get through it.
3. Your relationships with music. If you're obsessed with music and you're listening to some new music, while doing a test, homework or something else, you will waste the major resource of your mind on following up, recognition of patterns and rhythm and so on and so forth. Personally, i never listen to new music during "concentration moments" because i deeply focus on music itself which is harmful for learning process. Some background music you have listened to for a while might do the trick for you though.
As for me, i said earlier that i love music. Especially EDM (melodic dubstep, idnie electronica, progressive house, future bass), ambient (future garage mostly, though) and hip-hop. What i have noticed i that i can't focus at all while listening to EDM - i'm getting obsessed (especially if it's Illenium, Porter Robinson, Unlike Pluto and some other favorites producers). Everything else seems fine for me on the background. Even some dirty metal or hardcore