The horse in nightmare is an accident of spelling. The "mare" in the word has nothing to do with the female horse. It was a demon.

In Germanic folklore, the mare was a malicious spirit that came at night and sat on a sleeper's chest. It pressed down until the victim struggled for breath, sweated and could not move. Old English called it mære. Wikipedia traces the word to Proto-Germanic marōn, and scholars trace it to an Indo-European root associated with crushing and pressing. The horse word, from a different Germanic source, only happens to look the same.

The belief was widespread. Cognates run through Dutch, German and the Scandinavian languages, and the Slavic world had its own version, as in Polish zmora. French borrowed the Germanic element and made cauchemar. In the 13th-century Norse Ynglinga saga, a sorceress sends a mara to torment King Vanlandi of Uppsala, and it kills him in his sleep.

When "nightmare" appears in English around 1300, it names the spirit, not the dream. Etymonline defines that first sense as an evil female spirit that afflicted sleepers with a feeling of suffocation. Its victims, in that definition, were men or horses. An 11th-century gloss even paired Latin Echo with an Old English wudumær, a "wood-mare," as tormenting spirits: one in the wilderness, the other in sleep.

The meaning then moved step by step. By the mid-1500s, a nightmare was the crushing sensation rather than the creature. By 1829 it could be any bad dream. From 1831 it could be any very distressing experience, the sense in "a nightmare commute." Meanwhile the plain word "mare" for the spirit faded away, leaving only the compound.

The folklore probably reflects real episodes of sleep paralysis: waking unable to move, with a sense of pressure on the chest. That would explain why the mare was imagined sitting rather than chasing.

The horse did eventually join the story, through art. In 1781 Henry Fuseli painted The Nightmare, showing an ape-like incubus crouched on a sleeping woman. A horse's head pushes through the curtains behind her. Critics have read that horse as a visual pun on the word, which means the confusion is at least two centuries old.