I own a cactus of unknown type in the Opuntia genus.
It grows long spines, however, where the leaves are close, the spines curve away so that the cactus avoids impaling it's self.

How does one leaf 'know' to curve it's spines away to avoid impaling it's neighbouring leaves?

Good question. The spines are not hard when they first appear, and only toughen up when they have fully developed. They are therefore probably bent away from the other parts of the plant before becoming hard. This is easier to understand if you know that the spines are actually modified leaves (the 'leaves' of a cactus are actually stems) and therefore they can change during development.