The first viruses were not actually seen until the invention of the electron microscope in the 1930s. But for centuries before that discovery, scientists sought ways to contend with epidemics. Some, like Louis Pasteur, sought vaccines for pathogens they could not see but knew existed. Others, like Carlos Finlay who studied Yellow Fever in Cuba, and speculated about transmission (and how to stop mosquitoes from spreading that illness). But even before that, societies felt the impact of epidemics and learned that they tended to come from ships. Trade ships sailed all over the world, bringing back treasures and silks and spices -- and sometimes the plague.
The notion of quarantine seems to have originated in cities near the Mediterranean Sea that were major trade hubs in the 14th Century. These cities recognized that disease tended to follow the landing of ships and asked arriving ships to keep their goods and sailors at sea for 30-40 days after arrival to make sure that they were disease free. The first reported "quarantine" was in Dubrovnik, a port city in Croatia. Lasting only 30 days, the waiting period was technically a trentino.
Venice and Milan, in Italy, were also critical port cities of the period, and lost thousands of people (as many as 25-40% of their populations) to waves of Bubonic Plague. Like the Croatians, they did not really understand what caused plague (and to be sure, some thought it was witchcraft or a punishment from God), but they did see a connection with disease and ships arriving from foreign lands. They instituted 40-day waiting periods: quarantinos - the precursors to our quarantines, which retain the notion of isolation, if not the 40 day span. It's unclear how they chose 40 days. They did not have a notion of how long a disease could lay dormant in a person. But, 40 is an important number in the Christian bible and as the History Channel notes, women were already asked to remain confined for 40 days after having a baby. So, 40 seemed like a number that was both blessed and associated with health.
The history of quarantines makes you think hard about science. So much discovery is merely stumbled upon. The people of the middle ages did not know about micro-organisms or incubation periods. They did not know what bacteria or viruses were. But, they had a sense that disease might come from other people, and that you could wait it out. Who knew that they would stumble on a solution that still has merit now, hundreds of years and a myriad of scientific discoveries later?
For a quick read on quarantines, the History Channel website is helpful: https://www.history.com/news/quarantine-black-death-medieval
For an wonderful literary take on a real life plague village, check out Geraldine Brooks's Year of Wonders: https://www.amazon.com/dp/B0029WILXK/