"Free" in freelance never meant free of charge. It meant unattached.
The term was a literary creation. Etymonline calls "free-lance" a compound of free and lance apparently coined by Sir Walter Scott, who used it in Ivanhoe. Merriam-Webster dates the noun to 1819, and Etymonline gives 1820, so the two sources differ by a year. "Apparently coined" is the careful wording, because a dictionary can only show the earliest use it has found.
In Scott's novel, a free lance is a medieval mercenary warrior. The name describes a lance that is not sworn to any lord. Etymonline compares the idea to the Italian condottieri, the captains who hired out their soldiers to whoever paid.
The figurative sense arrived in the 1860s. Etymonline gives 1864 for the broader use, applied to people unattached to a cause, and by 1882 it was being used specifically of journalism. The writer who sold work to many papers, rather than one, was borrowing the picture of a knight who fought for many masters.
Merriam-Webster glosses the original sense as a mercenary soldier. The lance is a real weapon, and the whole phrase pictured a fighter who could be hired by whoever paid.
The grammar grew in stages. Merriam-Webster records the adjective in 1891 and the verb in 1902. The agent noun freelancer is earlier in Etymonline, dated 1898.
The metaphor fits. A mercenary sold fighting skill by the job and had no standing loyalty. A modern freelancer sells writing, design or code the same way.