Why does $(g^a \bmod n)^b = (g^b \bmod n)^a = g^{ab} \bmod n $? The Diffie–Hellman key exchange protocol relies on the fact that one person, Alice, can perform $(g^a \bmod n)^b $ and another person, Bob, can perform $(g^b \bmod n)^a $ and they will both arrive at the same number: $g^{ab} \bmod n $. This allows Alice and Bob to create a private, yet symmetric, key. Why is this identity true?

It seems that you are thinking of 'mod' as an operation, such as might be found in a programming language. If, instead, you restate the above identity in terms of congruences, it might be clearer:
$$(g^a)^b  \equiv g^{ab} \equiv (g^b)^a \pmod n$$
