Songwriting and genealogy
The best tool for understanding where music comes from is evolutionary biology. Songs don't spontaneously spring into being any more than animals or plants do. They evolve, descending from reshuffled pieces of existing songs, the way our genes are shuffled together from our parents' genes. The same way that all life has a single common ancestor, all human music has a shared origin in the calls of our primate forebears.
You can trace the ancestry of music like you can trace the ancestry of a person
Each new song is built using the same modular components as the other songs of its time and place, the way that all humans share the same genetic toolkit. My sister and I are like two different songs from the same album by the same band. My cousins are like songs on different albums by bands with overlapping members.
The ancestry of music is more complicated than the ancestry of humans. A better model for music is the evolution of microbes, with a lot of horizontal gene transfer happening. Biologists use the term "gene cassettes" to describe the semi-self-contained hunks of DNA that bacteria swap back and forth. The analogy to music fans spreading memes by passing tapes around couldn't be any more perfect. Some musical relationships do conveniently lend themselves to family tree-like representation. The practice of sampling and quoting existing songs creates a particularly clear and unambiguous set of relationships well-suited to network diagramming. The internet has several handy sample databases, including Whosampled.com and Wikipedia. I've been hard at work the past year or so making sample maps visualizing the more interesting chunks of data.
Sampling is the easiest set of relationships to diagram, but I could draw similar charts for use of particular scales, chords, rhythmic figures, melodic motifs, rhyme schemes, combinations of instrument sounds, and all the other memetic nuts and bolts of music.
A few really successful memes make up most of the music we hear
Some musical memes are better at getting themselves copied than others, the way genes for color vision or opposable thumbs are good at getting themselves copied. Here in America, the most successful memes include the backbeat, the one-four-five chord progression and the blues scale.
To illustrate just how widespread a musical meme can get, here's a video called "Four Chords, Thirty-Six Songs." In the key of C, the four chords are C, G7, Am, F. (There's some coarse language towards the end.)
https://www.youtube.com/watch?v=i4_f6pfabQk The video barely scratches the surface of all the songs, famous and not, that have used those four chords. So why is this chord progression such a big hit? For one thing, it's easy to play on piano or guitar or whatever. For another, the four chords sound good in any sequence or combination, spaced out on any harmonic rhythm. They have a wistful yet still uplifting mood that suits a variety of musical statements in a variety of styles.
Computers make recombining and resequencing the memes effortless
Pre-computer, composing and recording a song was a slow and effortful process. You wrote the song out or memorized it. Then you got a band together and they read the song, or you repeated it to them until they memorized it. Then you rehearsed it a bunch, and then recorded it from beginning to end. Sometimes you had to record many takes to get a good one. To get a polished, professional-sounding result generally required expensive gear operated by highly specialized engineers. You can still operate that way if you want, but computers offer some faster and easier alternatives. I prefer to write by improvising into the sequencer or digital audio editor, picking the best patterns and editing them into shape. The computer gratifyingly collapses improvising, composing and recording into a single act. Making music electronically is like being able to type out any DNA sequence you want and immediately seeing how it will look as an organism. You can skip the tedious embryonic development of notating, rehearsing and memorizing. Technologies like MIDI, sampling and pitch-detection software let you read any existing musical genome and resequence it to your heart's content. All this freedom is positively alarming to some of the musicians I know, who view it as evil or immoral in some way. I find that the computer eliminates some of the labor, but doesn't do the imaginative work for you. The computer makes it effortless to spin out ideas, but you still need to select among them and decide which are the good ones. The creative act itself stays the same as it always has been; there's just less friction.
Towards a unified theory of musical evolution
A genome is an algorithm for getting itself copied by generating the proteins and other structures making up an organism. A group of memes (a memeplex, as Susan Blackmore puts it) is an algorithm for getting itself copied by generating performances and recordings. What makes a song likelier to get itself heard, and eventually copied or adapted? Exact copying of previous generations of songs is a bad long-term strategy. Tastes change, like the way the environment changes for organisms. A meme that was successful yesterday may not be successful tomorrow. Total originality is a bad strategy too. It's easy to be original, to create a piece of music with no precedent or borrowing from anything existing. Bang randomly on a piano and you're probably going to play something that's never been played before. It's likely that your random banging will mostly be annoying. Chances are, a random DNA sequence won't make for much of an organism either. To be liked enough to be copied and imitated, your song will need to be substantially familiar. Forming an emotional connection with the listener requires a lot of shared vocabulary and associations. What works the best in music, as in biology, is a minor mutation on an existing successful replicator. Most mutations will make it harder to get copied, but a lucky few improve your chances dramatically. See a version of this post on Quora.