One common misconception about the evolutionary model and natural selection is that it always leads to greater fitness.
It's fostered by the way the subject is taught; that evolution always works by "survival of the fittest," so over time organisms always get bigger, stronger, faster, and smarter. There's a kernel of truth there, of course; natural selection does favor the organisms that survive longer and have more offspring. But there are a number of complicating factors that make this oversimplified impression far wide of the truth.
First, evolution -- in the sense of changes in gene frequencies in populations -- can happen for other reasons besides fitness-based natural selection. One common example is sexual selection, when a characteristic having little to nothing to do with fitness is the basis for mate choice, such as bright color in the males of many bird species. This has led to sexual dimorphism -- males and females have dramatically different external appearance, as the females choose ornately-adorned mates who then pass those genes on to their offspring.
Second, the selecting factors can change when the environment does. What is an advantage today can be a disadvantage tomorrow. This can lead to what's called an evolutionary misfire -- when some characteristic that was beneficial turns deadly as the conditions change. A particularly interesting example of this is the way moths navigate. Moths, like most insects, have compound eyes, hemispherical structures with multiple radially-oriented facets. They find their way around at night by using distant light sources (such as stars and the Moon), keeping the light from those objects shining into one facet of the eye. If the light source is distant, this allows the moth to fly in a straight line. The problem is, moths evolved in a context where there were no nearby light sources at night; and if you try this orientation trick on a close-up light source, you end up flying in circles.
And this is why moths spiral around, and eventually get fried by, candles, lanterns, and streetlights.
Third, many -- probably most -- genes have multiple effects, a phenomenon called pleiotropy. A single gene that can provide a benefit in one way can provide a serious disadvantage in another. This is, in fact, why this topic comes up today; scientists at the University of Chicago showed that a gene -- ERAP2 -- that gave people in the fourteenth century a better shot at surviving the bubonic plague also predisposes them (and their descendants) to Crohn's disease.
The link, of course, is the immune system; Crohn's is an autoimmune disease, one where the body's immune system starts fighting against its own tissue, in this case the lining of the digestive tract. The improvement in survival during the Black Death was significant -- an estimated forty percent increase in the chances of fighting off the disease -- so during the pandemic, the increase in risk for Crohn's was an acceptable tradeoff for near certain death from the plague.
Evolution is, truly, "the law of whatever works at the time." It's not forward-looking; the forces of selection act on whatever genes the population has currently got, and the set of genes which is able to make more copies of itself is the one that gets passed on to the next generation. This can happen because of reasons of fitness, but for many other causes including pure random chance (what is called genetic drift).
And because of this, the genetic hand we were dealt can sometimes have untoward consequences -- in a world where the bubonic plague has in most places been eradicated, a gene that had a beneficial effect now turns out to be a significant disadvantage.