One idea historians have is that flea market comes from the the outdoor bazaars of Paris, some of which have been around for hundreds of years. According to the association that runs one of the markets today, the term first sprang up in the 1880s when an unknown bargain hunter looked upon the market with its rags and old furniture and dubbed it le marché aux puces (“market of fleas”), because of shoppers’ perceptions that some of the more time-worn wares sold there carried the little bloodsuckers. The first recorded appearance in English that the Oxford English Dictionary lists, from 1922, makes reference to this origin.
Another possible origin has its roots in the same French markets, but with a twist on the words and meaning. As the city planners of Paris began laying down its broad avenues and constructing new buildings, some of the side streets and alleyways that were home to the second-hand outdoor markets and stalls were demolished. The merchants were forced to take their wares and set up shop elsewhere. Once reestablished, the exiled bazaars came to be known, in English, as “flee” markets, which somehow got turned into “flea” later on (though no one seems to have an explanation for why).
A third explanation comes from colonial America. The Dutch traders who settled New York had an outdoor market they called the Vlaie (sometimes spelled as Vly, or Vlie) Market, named from the Dutch word for “swamp” and referencing the market’s location on what was once a salt marsh. English speakers pronounced the word with an “f” up front (update 12/6: and sometimes a long "I" on the end), and the Fly/Flea Market and other places like it eventually all became flea markets.