The question is so broad I don't think it can be answered accurately. In terms of melody and harmony, there is a basis in physics for the concept of the "octave", where doubling or halving a note's frequency of vibration will result in a note that sounds as though it has the same identity, despite being higher or lower. So that is the finite source of pitches which we work from. The division of that octave into sub-pitches, called "notes", also has a basis in physics which is the harmonic series. Any given pitch when sounded will include in its body a subtle series of sympathetic pitches arising from the first. These pitches exist in mathematical proportion to the first pitch, and much of harmony seems to stem directly from the consequences of this series. In other words, when adding a note to a given structure, the lowest note of that structure tends to be the anchor note and every note added tends to take a place in the structure that is consonant or dissonant, and the consonance of the note tends to be somewhat related to that note's position in the harmonic series of the anchor note and other notes.
The decision to divide the octave into 12 notes, and the rules (called temperament) deciding exactly how wide the intervals are between them and specifically what their relationship are, appear somewhat based on the harmonic series but also arbitrarily resting on the prevailing conventions of harmony at that time. In other words, certain chords and progressions of chords entered common use, and the 12-note scale tends to support those chords and is thus an invention of Western music but not the authoritative law of how the octave should be divided. Alternative systems exist, and today some composers are writing "microtonal" music that uses an octave divided into 19 or more parts.
Once the question of how to divide the octave is settled, there is a much more limited set of choices for creating harmony and melody. These notes will necessarily result in certain structures which are consonant and those which are dissonant; chord progressions which are release tension and those that do not. Western music theory, as based on a 12-note equal temperament scale, is then based on the hundreds of years of practice, performance and composition that ensued after the 12-note scale was devised, seeking to understand these relationships of consonance, dissonance, resolution and tension arising from the result of a harmony built on the 12-tone scale.