The notes that sound good together have a basis in the harmonic series, the 1st and 3rd are octaves of the fundamental, 2x an 3x the frequency. the 2nd harmonic and 4th harmonic make up the other 2 notes in a major chord. Periodic sounds that are not pure sine waves (like vibrating strings) have harmonics. Our ears are attuned to hear these harmonics and this natural phenomenon is the basis of all pitch, melody and harmony in music.
https://en.wikipedia.org/wiki/Harmonic_series_(music)