...and does it have anything to do with horses?
Actually not. The word "mare" in this sense means "demon" or "goblin" - so, a night demon, an evil spirit that gives you horrible dreams.
That usage of the word "mare" faded out, and only the compound form survived, although for a long time it still referred to the demon or spirit, not the dream itself.
But nothing to do with horses at all.