First, director make the movie. Then the director and composer talk about every scene and character. The composer write and choose the music for each scene in the movie. When the music dosen't fit the music perfectly, the director cuts or changes part of scene. this way the music matches the movie in perfectly.