The earliest known quarantine law did not last forty days. It lasted thirty.
In 1377 the Adriatic port of Ragusa, now Dubrovnik in Croatia, passed a decree against the plague. Ships and travelers from infected places had to stay apart for a month before entering the city. The waiting period was called a trentina, from the Italian for thirty. Anyone who broke the rule could be fined, and bringing food to people in isolation without permission was forbidden.
Thirty days turned into forty. A review of quarantine history in the Journal of Infection says the longer period was probably adopted because a month was not considered enough to stop the disease. Wikipedia dates the Venetian Senate's move to forty days to 1448. According to the same review, Venice had already opened one of the first known lazarettos, an island quarantine station, in 1423. Its system became a model for the rest of Europe.
The Venetian word for that stretch of time, quarantena, from quaranta "forty," is the one English kept.
Why forty? Nobody knows for sure. Snopes lists four explanations that historians have offered. One is simple efficacy: the shorter wait failed. Another is the forty days of Lent. A third is the biblical forty, as in the flood or Christ's fast in the desert. The last is medical. Hippocratic doctors taught that an acute illness would show itself within forty days. Any of these could have shaped the choice, and they are not mutually exclusive.
English actually had the word before it had the practice. Etymonline records quarentyne in the 15th century for the desert where Christ fasted forty days. From the 1520s, a quarantine was also a legal term: the forty days a widow could stay in her late husband's house.
The plague sense arrived in the 1660s, naming the period a suspect ship was held in isolation. Within a decade it had widened to any period of forced isolation. The verb, to quarantine someone, followed in 1804, and it soon took on a figurative sense: to cut off or isolate by authority.
So the number in the word is historically real, even though the very first version of the rule used a different one.