How similar is psilocybin to the body's own substance serotonin? Why does the chemical structure of psilocybin resemble that of serotonin?
Psilocybin already looks very similar to serotonin at first glance, but is converted quite quickly in the body into the active substance psilocin, and then it resembles serotonin even more.

Because the phosphate group is cleaved off and a hydroxyl group remains, it resembles serotonin more closely. You can clearly see the structural similarities in the image below. In serotonin, the hydroxyl group (OH) is located at the 5th position of the tryptamine, whereas in psilocin, it is at the 4th position.

The chemical name of serotonin is 3-(2-amino-ethyl)-1H-indool-5-ol or simplified 5-hydroxytryptamine.
The chemical name of psilocin is 3-(2-dimethylaminoethyl)-1H-indool-4-ol or simplified 4-hydroxy-dimethyl-tryptamine
From the chemical structures and naming, you can already deduce that these substances resemble each other. In addition to the above difference regarding the location of the OH group, psilocin also has two methyl groups attached to the nitrogen (NH2) at the end of the tail.
Structurally, psilocin and serotonin resemble each other, but because their shape is slightly different, both substances will fit a fair number of the same receptors. Due to these differences, their activity on receptors can still vary and be just slightly different. For example, psilocin is only a partial agonist for the 5HT1A receptor, while it can bind well to 5HT2A.
I would like to add a few more interesting biochemical details to Marcel's good explanation. The structural similarity is indeed fascinating.
First, a word about those methyl groups that Marcel mentions. These two methyl groups on the nitrogen are actually very important for pharmacology. They make psilocin much more lipophilic—that is, fattier—allowing it to cross the blood-brain barrier more easily than serotonin itself. This is actually the reason why psilocybin can have an internal psychoactive effect and serotonin cannot. Serotonin could theoretically activate the same receptors, but it cannot cross the blood-brain barrier due to a lack of that lipid protection.
Also interesting: the position of the hydroxyl group at position 4 versus position 5 subtly influences how the molecule fits into the receptor axis. This difference in 3D conformation is likely why psilocin is a full agonist for 5HT2A receptors, whereas serotonin is only a partial agonist there. This difference in the agonism/antagonism profile is actually why the psychoactive effects are so strong.
Also: all these substances belong to the tryptamine class. This means that they all share the same basic building block – an indole-shaped core molecule with an ethylamine tail. This is also the reason why many different psychedelics – LSD, DMT, psilocybin, mescaline – all have fairly similar receptor profiles, despite coming from very different sources.
And finally: this is a great example of why pharmacology is not just about what enters your body, but also about the subtle structural differences that can determine whether or not a molecule is psychoactive, lipophilic, or able to pass through certain barriers. This is why chemical analogues of psychedelics are so interesting and why scientists are now searching for new variants.
Psilocybin indeed has a similar basic structure to serotonin—both are indoles with methyl groups. This allows psilocybin to activate serotonin receptors. The structural difference lies in the exact positioning of atoms, which determines how well it binds to receptors.