The readers never know, but the story and the music intertwine in the writer's memory, so when the writer hears the music later, the writer remembers the story... and if you read the story, sometimes you hear the music.
Other works seem to require silence.
Now, I'm writing my current novel (Point of Divergence, which the other authors of this blog know well) while listening to "electronic breakbeat jazz" artist Jonah Dempcy, also known as Revolution Void.
He's not for everyone, but since I really like widgets (and because maybe now I can listen on my own blog if my cute little red refurbished Sansa mp3 player ever bites the dust), I put a Jamendo widget below that plays his music.
