The Giant Spider is immune to all mind-affecting spells. To me this seems arbitrary and I was wondering if there was a in-world justification for it, or if it's a reference to some other story or mythological character, or a reference to some aspect of spider biology.
Mechanically: Because it's a Vermin.
The Giant Spider itself does not have that ability, but rather inherits it from its creature type: Vermin. Vermin have the following trait:
Mindless: No Intelligence score, and immunity to all mind-affecting effects (charms, compulsions, morale effects, patterns, and phantasms). Mindless creatures have no feats or skills. A vermin-like creature with an Intelligence score is usually either an animal or a magical beast, depending on its other abilities.
So, because it's a vermin, the Giant Spider is immune to all mind-affecting effects.
Biologically: Because it hasn't got a brain.
So obviously it has a brain of some form. It moves, it does things, it mates, but the game assumes that its brain is simply so different from the brains of humans and other animals that the spells developed to target higher-order functioning, advanced pattern recognition, and emotions don't work. It's like targeting a snake with a spell that causes limbs to fall off but doesn't affect the spine. I don't have that body part, therefore I'm immune to your spell.
Why that got expanded out to every other level of mind-affecting effects: simplicity.
By making a blanket statement and sticking to it, the developers avoided the problem of having to re-think every monster that's like a relatively mindless, emotionless insect. Vermin are so stupid they can't be fooled or commanded. Done. Oops, we stopped a "Control Vermin" spell right in its tracks that players will be so keen to have for a type of creature that's rare and undesireable in most games.
Because Pathfinder is derived from Dungeons and Dragons 3rd edition
Spiders did not have this feature prior to the 3rd edition of D&D. 3rd edition tried to systematize monsters by giving traits to monster types so that similar monsters had similar features.
Spiders were "Vermin" and vermin had the trait "Mindless: No Intelligence score, and immunity to all mind-affecting effects (charms, compulsions, phantasms, patterns, and morale effects)."