What do I mean by "historically and logically"? Though our knowledge of music history is certainly far from complete, with many things unknown, it seems fairly clear that, as far as Western European music goes, melody came first, followed by counterpoint and finally, harmony. Rhythm, of course, was always there--indeed, there may have been rhythm before anything else, but it was not written down.
The way we encounter music in our own lives is usually quite different. A lot of us start out by playing a few chords on guitar to accompany ourselves singing, or we start singing and then learn a few chords. In either case we encounter harmony as a series of strummed chords, which conceals its origin and fundamental nature.
Harmony is the byproduct of counterpoint!
Historically how it came about was by composing additional music to go along with the chant of the church. This started to be written down early in the second millennium as it wasn't until the invention of the lines of the staff by Guido of Arezzo that notation became capable of notating exact pitches. Here is what one of the earliest examples of counterpoint looked like:
There are two staves there, an upper one for a quicker moving melody, and a lower one, the original chant. A heavy horizontal line divides them. Here is what this looks like in modern notation:
As you can see, how to notate rhythms accurately has not yet been discovered. I don't have this for you in a performance, but a bit later, Pérotin of the School of Notre Dame composed an organum on the same text. Here is his Viderunt Omnes performed by the Hilliard Ensemble with the score:
This is the history of music, as it was written down at least: first were the melodies of Gregorian Chant that were sung unaccompanied. Sometime after 1000 AD, monks and church musicians started improvising decorative melodic lines to go with the chants, note against note. The original phrase in Latin was "punctus contra punctum" or "note against note" and from this comes our term "counterpoint". At first they just sang lines that were parallel to the original melody, but the creativity of the musicians soon resulted in melodies that were very different from one another. They discovered some of the fundamental principles of counterpoint such as that voices sound particularly good when they move independently of one another: contrary motion, it's called.
"Harmony" is simply that which results when you have two or more notes--voices--sounding together. The basic idea of consonance and dissonance also comes from these early examples. At first voices had to start and end in unison and in between mostly used fourths and fifths. Then thirds and sixths became acceptable. But right up until the end (temporary, at least) of tonal harmony early in the 20th century, the underlying structure of harmony goes right back to the original counterpoint. Let's take a basic cadence as an example. Here is a I ii6 V7 I cadence in C major:
And here is what it sounds like:
Now here is exactly the same cadence with exactly the same notes, but written out for voices so you can see that each voice has an independent melodic line:
And here is what that sounds like:
Those aren't real voices, of course, just synthesized voices from my music software. Notice how the upper three voices move by step and the bass has leaps. Notice how the soprano descends to the final C while the alto, with the B leading tone, rises to the C. Notice how the F, the seventh of the dominant seventh, in the tenor, falls to the E. And notice how the bass leaps from G to C. These are all fundamentals of counterpoint: leading tones always move up to the tonic and sevenths always go down to the third of the tonic.
In popular music all these contrapuntal fundamentals are obscured because pop musicians typically just think in terms of strummed chord progressions with a vocal melody and, perhaps, a bass line. Sure, there is always a bass line, but it often just follows typical formulas instead of being "composed" by the songwriter.
So that's a little insight into the relationship between counterpoint and harmony. Basically, all harmony comes from counterpoint. Let's listen to some Bach for an example. Here is Andras Schiff with the Partita No. 2 for keyboard: