When we are not fully engaged in a task or a task is too easy/boring, your mind wanders and reduces productivity. Mind wanders because there is un-used attention. Music can occupy that surplus attention and help you focus.
Different types of music disrupts memory and attention. Vocals and speech disrupt the most. Instrumental music disrupts less. Silence and noise work equally well for most. Instruments like sax and electric guitar can be as distracting as vocals due to overlapping frequencies.
Through a process called "stochastic resonance", musical signals to the ear amplify signaling in the brain, which increases neural firing efficiency. Adding musical noise strengthens weak neural signals. This helps with creative thinking by activating more neural connections at random.
For difficult tasks, don't use music. For easy tasks, use music. Adjust the volume and choose songs based on how much "load" there is on the brain. Medium load works best. Too little load or too much is bad for learning.
Choosing music according to your preference when you have beliefs like "EDM helps me focus" or "metal is distracting" matters the most. Your beliefs determine how effective music is to help you focus.
ADHD is likely caused by low dopamine activity, and music can increase it through the anticipation of musical sequences/elements and the peak emotions created by music. White noise can also help ADHD children but hamper non-ADHD children while doing cognitive work like calculations, memorizing, analysis, etc.