There are a few important principles.
The first principle (not sure if it has a name) is if you connect together all the inputs of an and/or gate you get a gate that passes the input signal through unchanged. Similarly if you set all but one of the inputs of an AND gate to 1 or all but one the inputs of an OR gate to 0 you also get a gate that passes the input through unchanged. It should follow pretty obviously from here how you can turn a NAND or NOR gate into an inverter.
The second principle is known as "de-morgan's theorem", basically if you add inverters to the inputs and outputs of an AND gate you turn it into an OR gate and vice-versa. Putting this together with the first principle it should be pretty easy to see how you can make an AND, OR or NOR gate out of NAND gates.
The third principle which also follows from de-morgan's therem and which is important in dealing with more complex cases is that NAND followed by NAND is equivilent to AND followed by OR.
This gives us a strategy, if we can express something in terms of AND followed by OR (known as "sum of products" form, since OR is considered analagous to addition and AND is considered analagous to multiplication. we can implement it with at most three layers of NAND gates. The first layer used as inverters, the second layer used to perform the AND function and the third layer used to perform the OR function.
And we can express anything in "sum of products form", just go down the truth table, if the output is 0 ignore the line, if the output is 1 write a term representing that line. Lets try and use this to build an XOR.
The truth table for an XOR looks like
A B Q
0 0 0
1 0 1
0 1 1
1 1 0
So now we can write out our equation in sum of products form.
$$ Q = A \oplus B = A.\overline{B} + B.\overline{A}$$
In more complex cases we may want to simplify the sum of products form by combining terms before we implement it with NAND gates. There are various techniques for doing this but they don't help with XOR. The sum of products form is already minimal. So lets go ahead and translate it to NAND gates.
$$ Q = \overline{\overline{A.\overline{B}} . \overline{B.\overline{A}}}= \overline{\overline{A.\overline{B.B}} . \overline{B.\overline{A.A}}} = \overline{\overline{A.\overline{1.B}} . \overline{B.\overline{1.A}}}$$
So now we have a valid implementation of XOR using NAND gates. I think if you were doing it for yourself you would be more than entitled to stop here.
However there is one trick left we can use to simplify the NAND gate implementation of XOR. It turns out that in the above formula rather than replacing \$\overline{A}\$ with \$\overline{A.A}\$ or \$\overline{1.A}\$ and replacing \$\overline{B}\$ with \$\overline{B.B}\$ or \$\overline{1.B}\$ we can replace both of them with \$\overline{A.B}\$. We can do this because the value of \$\overline{A}\$ only matters when \$B\$ is 1 and the value of \$\overline{B}\$ only matters when \$A\$ is one.
$$ Q = \overline{\overline{A.\overline{A.B}} . \overline{B.\overline{A.B}}}$$