Poop contains water, bacteria, undigested material, shed intestinal cells, and many other substances.
The smell comes from a much smaller set of chemicals mixed into that material.
Gut bacteria make many odor compounds
Your large intestine contains an enormous community of microbes.
They break down material that was not fully absorbed earlier in digestion.
During that process, they produce gases and other chemicals.
Some smell much stronger than others.
Sulfur compounds matter
Certain sulfur-containing compounds have extremely powerful odors even at very low concentrations.
That is part of the reason a relatively small amount can make the entire bathroom aware of what happened.
Other compounds contribute too.
There is no single “poop smell molecule.”
Why does diet change the smell?
Different foods deliver different materials to gut microbes.
That changes what the microbes can ferment and what chemical byproducts they produce.
Foods rich in sulfur-containing compounds can sometimes make odors noticeably stronger.
Why does poop smell worse when you are sick?
Infections, rapid intestinal transit, changes in digestion, medications, and changes in gut bacteria can all alter stool odor.
A change in smell by itself usually does not identify the cause.
Is this related to why farts smell?
Very much so.
Both come partly from microbial activity in the gut.
Most intestinal gas is actually odorless.
The smell in a fart comes from small amounts of potent compounds.
That question will get its own explanation rather than being squeezed into why we fart.
Why do humans find the smell disgusting?
Disgust helps steer us away from material that can carry pathogens.
Feces can spread disease.
A strong negative response to its smell is not exactly a terrible survival feature.
Most of poop is not made of foul-smelling chemicals.
The problem is that your nose can detect some of them in ridiculously small amounts.
And that’s the short explanation.
Related explanations: Why do we fart?, Why does sweat smell?, and Why do feet smell?