Long hair is inherited from the common ancestors of humans and merpeople.
As whales evolved from an ancestor of the hippopotamus and manatees from an ancestor of the elephant, merpeople evolved from an ancestor of humans. Thus merpeople are a hominin species with aquatic adapted physiology and joined legs.
DJMethaneMan's answer to "Why would merfolk evolve arms?" suggests that a population of early hominins was forced to adapt to an aquatic lifestyle after being marooned on an island during deglaciation. This is reminiscent of the aquatic ape hypothesis promoted by Westenhöfer, Hardy, and Morgan, which proposes that humans' ancestors led a semiaquatic lifestyle. Humans have far less body hair than chimps, for example, and what hair they do have follows the flow of water over the body.
Taking the adaptation a step further are creatures in Scottish and Irish folklore known as selkies. On land, they appear human; in the sea, they wear sealskin swimsuits that men of the land tend to steal. Selkies have physiological adaptations akin to those of Michael Phelps and Gudlaugur Fridthorsson but can interbreed with humans. Merpeople have adaptations along similar lines but to an even greater extent, though like seals and dolphins, they'd still breathe air.
Occasionally humans are born with a limb difference called sirenomelia, in which both legs are fused into one hind limb. This limb has two femurs, four lower leg bones, and ten toes. In humans, it's associated with defects elsewhere, and few with with the condition survive infancy. (A photo of one survivor named Milagros Cerrón can be seen as a transitional form between humans and merpeople.) But among merpeople, sirenomelia is normal and beneficial, as it eliminates turbulence between the legs when performing a dolphin kick. So over the generations, having what amounts to one thick leg became fixed in that population.
So if split legs were selected out of the population, why hasn't hair also been selected out? Other answers explain several reasons for retaining it, which I'll summarize:
- Sexual selection: Mermen still find long hair attractive. In fact, if long hair is as maladaptive as some claim, it may invoke the handicap principle in the same way as a peacock's tail feathers.
- Mimicry: Attracting human sailors in order to cause ships to wreck and then plunder the ship's supplies.
- Obfuscation: Disguising themselves in the water by creating a "cloud of hair".
- Symbiosis: Kelp hair decorations attract sources of protein.
- Providing raw material for fishing lines, nets, and other tools, with more tensile strength than steel.
- Shading the head and neck while hauled out on land.
Any of these could explain the sighting of a mermaid using a salvaged dinner fork as an improvised "dinglehopper," their term for a hair pick.