Between 1860 and the domestication of cows, sheep and goats, people had to handle raw milk and dairy made from it. How come they didn't get terrible infections from it?
OK, let's talk about infectious disease transmission a bit here.
One of the most important things to understand about infectious disease is that it is not a perfectly deterministic process, there is a lot of variation due to all sorts of factors. This can result in different outcomes on an individual basis as well as different transmission and mortality/morbidity effects at a population level.
Let's zoom in onto an exposure/transmission scenario. First you need an infectious disease to already be existing in some host (whether human or animal) and for infectious particles (e.g. virions, bacteria, etc.) to be shed in a manner that exposes a susceptible individual to that disease in a way that could potentially cause infection. But this singular event doesn't necessarily cause infection, it's like a game of Russian roulette. Depending on the amount of infectious agent in the exposure there might be a larger or smaller chance of disease transmission. This is a population game. A single virion or a single bacteria rarely causes an infection in a host, it typically requires hundreds or even thousands of infectious organisms to make for an effective infectious dose, the larger the dose during an exposure event generally the more likely the chance of infection becomes. Norovirus, for example, is potentially infectious with an exposure as small as under 20 virions, which is very rare for infectious diseases and one of the reasons why it is so incredibly infectious. All of the different layers of your immune protections (from your skin to your mucus membranes to your active immune system proper) will fight against that attempt at infection. Depending on all the details here, the size of the infectious dose, the state of your immune system, your overall general health, etc, etc, etc, your body might fight off that attempt before it can even gain a beach head. Or it may be that the infection begins ramping up and your body has to catch up. Again, depending on a zillion factors there are a zillion different possible outcomes here. Potentially your body could have the upper hand and could fight off the infection without almost any noticeable external symptoms at all, and possibly it could do so in a way that results in acquisition of immunity against that infection. And there's an entire spectrum of outcomes here all the way from these mild or asymptomatic cases all the way to extreme illness with severe symptoms that could have an outcome as serious as lifelong disability or death. It all depends on the nature of the disease, the details of the infection itself, individual factors like personal health and immune response, and the details of the care that the person receives. For example, cholera infection typically results in diarrhea that can cause dehydration, but so long as the afflicted individual is able to remain hydrated (through oral or intravenous rehydration) they have a very high chance of surviving, but if they cannot then the disease can be deadly.
It's worth taking a brief detour here to talk about maternal antibodies, which are especially important in a pre-industrial setting. An adult mother that has developed immunity to a disease will have antibodies circulating in their blood which can fight against that disease. These antibodies can be passed from mother to child through the placenta during pregnancy and through breastfeeding after birth. A young child with the aid of maternal antibodies will have an advantage in fighting off infectious diseases that their mother has an immunity against. With the aid of those antibodies the child has a higher chance of successfully fighting off an infection of one of those diseases and in so doing they have a good chance of developing their own natural immunity to that disease and thus producing their own antibodies (which will continue to protect them after they have been weaned).
All of which is to say that even within a context where disease transmission is rampant, there are still natural mechanisms that provide some degree of protection against everyone experiencing the worst possible outcomes.
Now, let's look at disease transmission dynamics a bit. If you have a population (of either humans or animals) as potential disease hosts you still need some mechanism, some event, some history which results in a particular disease becoming prevalent in that population. And that usually requires some form of importation event from an outside population where the disease is circulating to the susceptible population. In general, the more highly connected a population is the easier it is for these transmission events to occur between different population nodes (cities, villages, etc.) What this means is that diseases will be spread differently depending on the interconnectivity of a population both with itself and with outside groups. If you have groups of small, mostly isolated populations with limited numbers of connections to the outside world then disease transmission can become more difficult and rarer or at least not as ubiquitous.
OK, so let's come back to drinking milk again. Historically it would not necessarily have been ubiquitous that any given sample of milk being drunk would have been contaminated in some way. Additionally, depending on the diseases in play individuals may have had resistances or immunities to them. Drinking fresh raw milk from a "clean" cow is theoretically perfectly safe, and historically many people who drank raw milk would have been lucky enough to have experienced that scenario. But, of course, that was not the case all of the time. With raw milk there are a number of potential pathogens that can result in illness such as salmonella, listeria, or even bovine tuberculosis. But this is still a Russian roulette scenario. You need to have these infectious agents end up in the milk, you need to have the milk sitting in the appropriate conditions to allow for them to multiply to potentially hazardous doses, and you need to consume the milk in a sufficient quantity to be at risk, and you need the dynamics of the "fight" with the infectious agents to result in illness. It may be that as an individual you just constantly dodge these bullets throughout your whole life, with only a few close calls and maybe a few instances of experiencing "food poisoning" from salmonella or something similar.
Not everyone is going to experience the worst possible outcomes. Nevertheless, some will. And to be clear food borne illnesses were super common historically, and they added substantially to the overall burden of infectious disease in the pre-industrial world. In combination with other factors like malnutrition such diseases contributed to extremely high rates of infant and childhood mortality, to the degree that maybe only around half of all children managed to survive to adulthood.
So, to be crystal clear, drinking raw milk (and untreated water) was historically very dangerous, and imposed a very high level of mortality on human populations back then, though people still managed to survive through luck and the limits of human immune defenses and physical health. Even infectious disease was so potent a killer of humans historically that many cities typically experienced negative population growth due to disease, and could only grow through in-migration.
But such diseases were so common that there was usually no way to be certain of the source of the disease. Only as sanitation improved and science advanced was it possible to trace individual outbreaks of food borne pathogens and begin to tackle their spread.
Wrapping things up: throughout much of history a lot of raw milk was consumed from cows in small scale, rural farms where there was a reasonable chance that the cows were not just walking petri dishes. And within that context much of the raw milk would have been consumed quickly and due to various factors of individual immunity and such-like there would have been a good chance of being able to consume that milk with no major negative health effects. However, this was a bit of a crap shoot, and many people would have experienced various "bad outcomes" from contaminated milk. This could range all the way from mild diarrhea that is just an inconvenience for a healthy, robust adult to a bout of severe enteric disease that could be life threatening for a child (and could also contributed to stunted growth and reduced health later in life, increasing susceptibility to other diseases and causes of death) or even to acquisition of bovine tuberculosis in humans, which is also life threatening. Historically these sorts of things were major contributors to the large burden of infectious disease and the very high rate of infant and child mortality in pre-industrial times, and they are still today (including consumption of untreated raw milk) significant causes of early death and disease in the developing world.
As population density picked up with the urbanization concomitant with industrialization especially in the 19th century the potential for raw milk consumption to become an even greater source of disease and death became much higher, but fortunately this occurred at the same time as advances in medicine and sanitation provided the means to control the problem through inspection, epidemiology and infection tracking, improved cleanliness and sanitary methods, as well as pasteurization and temperature control.
Additionally, secondary products like butter, yogurt, and cheese are not necessarily as susceptible to serving as vectors for disease transmission as raw milk. All of these things, including butter, would have involved the creation of cultures of lactobacilli that would drive down the pH of the dairy product and make it an inhospitable environment for pathogenic bacteria to survive in. While butter and cheese would also often be salted. These can still be the source of infection but generally it would be less likely.
The modern risks are not the same as the ancient risks. Many ancient milk-consumers did not significantly increase their risk of disease by using raw milk and dairy products made from raw milk.
First, what are the modern risks? We can group them into two main categories:
Food poisoning, usually from Campylobacter jejuni, E. coli (especially strains that produce Shiga toxin, such as Escherichia coli O157:H7), and Salmonella. Staphylococcus aureus and Listeria monocytogenes are also important potential pathogens in raw milk. Bacteria such as these are usually due to contamination of the milk during milking and later handling.
Zoonoses (diseases transmitted from non-human animals to humans). The most important are brucellosis (caused by various species of Brucella, such as B. abortus (from cattle) and B. melitensis (from goats and sheep)) and bovine tuberculosis (caused by Mycobacterium bovis, a close relative of Mycobacterium tuberculosis which causes conventional human tuberculosis). Milk can carry these bacteria if the animal giving the milk is infected.
The first of these categories, food poisoning, is responsible for most cases of raw milk/dairy borne illnesses in the West today. Raw milk/dairy also causes most of the cases of dairy-borne food poisoning in the West; in the USA, about 95% of dairy-borne food poisoning is due to raw milk/dairy, despite raw milk/dairy being a small fraction of the total dairy consumption.
In many countries, the risk of brucellosis and bovine tuberculosis is much lower in the past due to a much lower incidence of brucellosis and bovine tuberculosis in cattle. The risk of these diseases from pasteurised milk/dairy is essentially zero.
For both food poisoning and zoonoses, the modern risk is greatly increased by pooling the milk from many animals. This increases the chance of a batch of milk carrying these bacteria - if any of the milk in the batch has been contaminated by bacteria that can cause food poisoning or zoonotic bacteria, the whole batch is contaminated. Also, the large batch of milk goes to many consumers, exposing a larger number of consumers to these diseases. This effect can be seen in the modern pattern of food poisoning from raw dairy. In the USA, there are on average about 10 identified outbreaks of raw dairy related food poisoning per year, with each of these outbreaks resulting in, on average, about 15 known cases of illness (and many cases with mild symptoms will be unreported, so the actual cases per outbreak will be higher). The US consumption of raw milk has risen in recent years, and the number of outbreaks per year has been increasing.
While milk-transmitted zoonoses are rare in the West today, this was not always the case. Today, brucellosis is rare or eradicated in many countries, but infection rates of dairy cattle can be 10% or even higher in some countries. World-wide, there are about 500,000 reported cases of human brucellosis per year, and the total number of cases per year is estimated to be about 5-12 million. Currently, Syria has the highest infection rate, about 1,600 cases per year per million people. In comparison, the US incidence is about 0.4 cases per million (with most cases due to cheese (illegally) imported from Mexico, where the brucellosis rate is quite high). Mongolia and Kyrgyzstan, with much of the population still involved in herding, and consuming raw milk/dairy, can be used to estimate possible ancient incidences among herding peoples, based on their modern incidences of about 1,000 and 900 per million respectively. Noting the modern mortality rate of about 3%, the Mongolian incidence suggests that about 3-4% of the population might have suffered from brucellosis during their life, with about 0.1% dying from the disease (lifetime, not per year).
Bovine tuberculosis is also dangerous. In the USA in 1900, before the pasteurisation of milk was widespread, about 15,000 people (mostly children) died from the disease every year, corresponding to a lifetime risk of about 1%. (Deaths due to diseases that are much rarer and/or less dangerous now are a major reason why the US life expectancy in 1900 was only about 47.3 years.)
Second, why are the modern risks different from the ancient risks? Historically, among farming/herding peoples, most people were subsistence farmers/herders. Raw milk, where it was consumed, would be consumed shortly after milking (in the absence of refrigeration, it has a very short shelf life - only a few hours in hot weather). Even if the milk was contaminated, there would be little time for bacteria that cause food poisoning to reach dangerous levels. Today, the longer shelf life due to refrigeration, and the longer time needed for the milk to reach the consumer, can provide dangerous bacteria with time to multiply (especially if they multiply faster than the bacteria responsible for spoiling the milk). Further, people milking animals for their own consumption may well take more care to avoid contamination than if the milk is being produced for sale (in addition to careless handling resulting in contamination, adulteration of milk with unclean water was often a historical hazard with milk offered for sale). Generally, the risk due to handling and consuming raw milk/dairy would have been lower than the risk of handling and consuming raw meat - potentially risky, but far from being "very dangerous", if the milk is kept clean and not obtained from diseased animals.
Modern and recent historical rates of zoonoses suggests that lifetime mortality rates due to raw milk transmitted zoonoses might have been about 1%. While this would be considered quite high today, historically such diseases were just a few among many equally or even more dangerous diseases (e.g., early smallpox vaccination (variolation) mortality rates of about 1% were considered a great improvement over the 20-30% mortality rate due to smallpox). Further, farmers and herders whose cattle (or goats or sheep) were infected with bovine tuberculosis or brucellosis could (and sometimes did) catch the diseases via contact with their animals - avoiding raw dairy did not guarantee safety.
In summary, modern risks of food poisoning due to raw milk/dairy are higher than the historical risk for subsistence farmers due to the pooling of milk from multiple animals (and multiple farms) and longer storage time. (The highest risk appears to have been, and still is, with milk that has been carelessly handled and/or adulterated, as is sometimes (too often!) the case even today in many developing countries.)
The modern risk of zoonoses is lower on average, although this varies greatly between countries. However, our modern perception of risk is different - when many common diseases have lifetime mortality risks of about 1% or greater, avoiding raw dairy has little impact on life expectancy. Noting the modern casual acceptance of much higher risks due to "lifestyle diseases" (e.g., due to smoking, alcohol consumption, lack of exercise, etc.), it is likely that the risk due to consumption of raw milk/dairy was considered unimportant by most people historically (also, the connection between diseases such as bovine tuberculosis and raw milk consumption is easily missed due to the long incubation period, which would lower the perceived risk).
Given the low cost of pasteurisation and the resulting greatly increased safety (and also the longer shelf life of milk), and the insignificant impact on the nutritional content of the milk, it's easy to see why modern health authorities recommend the consumption of pasteurised milk and the avoidance of raw milk. This doesn't mean that raw milk is "very dangerous" (any more than raw meat is very dangerous (with the exception of meat from animals that are likely to be infected by salmonella or other dangerous pathogens)) - even if raw milk is only mildly dangerous, a simple and cheap way of reducing the risk by a factor of about 1,000 provides a significant improvement.