Methods used in song writing range from experimentation ("that sound good, keep that") to mathematical composition of patterns (see Johann Sebastian Bach or Wolfgang Amadeus Mozart).
Though often seen as the job of the band leader (usually the singer), or the band as a group, a lot of modern pop songs have been actually written by professional song writers, that make a living by creating songs for interpreting artists.