And why even before that, were the modes based on a W W H W W W H, why
not some other pattern of intervals?
Ultimately, this comes from ancient Greek scales. Ancient Greek scales were built in tetrachords, made up of four notes bounded at the ends by a perfect fourth (4:3) ratio. There were lots of variations in ancient Greece about exactly how to tune the notes in the middle of each tetrachord, but the outer notes were always in a perfect fourth ratio.
Greek scales were tuned starting from the highest note and going down. One system that emerged for tuning began by filling in two whole steps (tuned to a 9:8 ratio) in each tetrachord, and leaving the remaining interval (about a semitone in size) as sort of the "leftover bit." This became known as one standard diatonic tuning.
If you imagine a Greek scale starting at the E above middle C and tuning downward, you'd have a perfect fourth E-B. Filling in two whole steps tuning downward, you get E-D-C-B. Then you can insert a whole step to start your next tetrachord on A below middle C. (These are called disjunct tetrachords, as they don't share an endpoint.) This note, which we now think of as A below middle C, was known to the Greeks as mese, basically the middle note of the scale. Anyhow, now you build another tetrachord going down from the A to the E below. Then you fill in with two whole tones and the remainder to get A-G-F-E.
Put those two tetrachords together, and you have E-D-C-B-A-G-F-E, tuned roughly close to how we tune our scale today, and creating the pattern of whole steps and half steps that would later give us the scale used for the medieval modal system.
The Greeks would then have expanded further by adding tetrachords above and below that central octave. Continuing downward starting on the low E (below middle C) and using a conjunct tetrachord, where the two tetrachords meet on a single note E, you could create another tetrachord below to get E-B, filled in again with E-D-C-B. Below this low tetrachord, the Greeks added one more note, called proslambanomenos, which was considered the lowest note of the scale.
The Greeks didn't use letter notation, instead only names like mese and proslambanomenos. When Boethius translated Greek music theory into Latin, he reordered the scale to be ascending rather than thinking of it as descending. Thus, he called the first note "A," which is why that A is in the lowest space on the bass staff: it is the lowest note of the original scale.
In any case, you now had your system of whole steps and half steps arranged in an octave to create the medieval modes, derived from one prominent Greek scale. (There are other complexities I've skipped over, such as how mese could also have a conjunct tetrachord built above it in Greek theory, thus creating a D-C-B♭-A tetrachord which ultimately introduced the first accidental to medieval musical scales, but that's perhaps a different story.)
As to how Ionian and Aeolian modes came to dominate, I wouldn't say they did. Those are modal names. The major/minor system came out of the modes, but it's also a sort of parallel development. Modal music was still sung in churches and used to understand how chant worked well into the 18th and 19th centuries (and even today in some old-fashioned Catholic communities). The principles of mode have to do with melodic organization of chant and classifications of chant, as well as melodic formulas to sing chant.
Tonality, instead, has to do with harmonic practice and developed along with polyphonic music. Ultimately, the old modal scales sort of collapsed into three primary "supermodes" (which some modern music theorists call "tonalities") by the 16th century or so, one of which was sort of "major-like" and included variants like the Ionian, Mixolydian, and Lydian scales, and another of which was "minor-like" and tended to be like Aeolian or Dorian, though with leading tones for cadences. (The third possible emerging "tonality" was based on Phrygian, with its downward leading tone to tonic, but became increasingly archaic through the 16th century on.)
As to why our particular "major" and "minor" scales came to dominate? That's a really complicated question having to do with the gradual development of systems of keys (and key signatures) in the 17th and early 18th century. But the basic answer is that the old modes never really left. What I mean is that functional harmony in the 18th century at the beginning of modern "tonality" is rarely diatonic. It incorporates chromatic notes all over the place.
And what are those chromatic notes? Well, think of major key pieces by Bach. Assume we're in C major. Aside from the typical notes of the major scale C-D-E-F-G-A-B-C, what are the most common accidentals? First, there's F♯, which is used to make a strong cadence to dominant all over the place. The other most common accidental is B♭, which is used to lead toward the subdominant key and is frequently used toward the end of a piece by Bach as he's wrapping up. Now realize for a moment that these two accidentals are precisely the inflections that would be contained within the old "major modes," i.e., Lydian and Mixolydian. It's not that the old modes ever died out; their inflections remained as a quintessential part of common-practice tonal harmony. The Ionian scale is a sort of happy medium between the various possible chromatic inflections and thus came to dominate a lot of "major-ish" polyphonic music even in the late 16th century.
Meanwhile, for "minor-ish" modes, the choice was between Aeolian and Dorian, though neither scale worked well for polyphony with its frequent raised leading tones. And the distinction took a longer time to die off there. "Dorian" key signatures which lack a flatted 6th scale degree were common even up until the mid-1700s, well into what we think of as "tonal music" taking over. And anyone with even a passing familiarity with minor mode as practiced by baroque composers knows that the sixth scale degree is very unstable: it is flattened when it goes down to scale degree five, but it's raised in a lot of other circumstances. The "minor" key that finally became standard in tonality really had more to do with consistent key signatures than any significant choice between "Dorian" vs. "Aeolian." However, composers of the baroque really did love using the flattened scale degree 6 to 5 motion to give a poignant quality, as in lament bass lines, so ultimately the version with the flat sixth came to be the "standard" minor, at least for key signature purposes.