The Diffie-Hellman Key Exchange Protocol will exemplified in the following. The relatively small size of the parameters was intentionally chosen to facilitate a better comprehension.
At first the public parameters need to be set. They are called public because any third party may gain knowledge of them without weakening the security of the protocol. Alice and Bob agree on the following public parameters:
g = 4
p = 13
Now Alice and Bob each choose a secret number which is known only to either Alice or Bob. Alice chooses her secret number a while Bob chooses his secret number b:
a = 10
b = 8
Next, Alice and Bob have to compute their respective shared keys A and B:
A = ga mod p = 9
B = gb mod p = 3
Alice and Bob need to exchange their previously created shared keys to calculate the final common and secret session key. Alice sends over her shared key A to Bob while Bob sends over his shared key B to Alice:
During the last step of the procedure, Alice and Bob separately compute their respective session keys:
SAlice = Ba mod p = 3
SBob = Ab mod p = 3
If you exchange both shared keys A and B with the appropriate equations from the second to last paragraph, the following equations appear:
SAlice = Ba mod p = (gb)a mod p
SBob = Ab mod p = (ga)b mod p
Since you can execute the multiplication in exponents commutatively (even when calculating modulo),
(ga)b = ga b = gb a = (gb)a mod p
the two session keys separately computed by Alice and Bob must be identical:
(gb)a mod p = (ga)b mod p
and thus
SAlice = SBob
Another example with concrete small numbers can be found in the book "Computer Security. Art and Science", © 2003 by Matt Bishop.