The king is invaluable. If it is attacked then it must move out of danger immediately
and if it cannot be made safe then the game is lost. At the beginning of a chess game
there is usually a battle for the centre of the board, therefore it is a good idea to move
the king from the action to a safe place in the corner and to move the rook to the middle
of the board where it can take part in the battle. Castling allows you to do this.
The chessboard can be divided vertically into two parts. Looking at the board from White's side, the left side is called the queen's side and the right-side is the king's side. These terms are important because the king has a choice of which corner he goes to; he can castle to the King's side or to the Queen's side.
When the king castles, he moves two squares to the left or right and the rook is moved to stand on the opposite side of the king. When castling, the king must always be moved first otherwise your opponent could claim that you have moved the rook instead of castling! In the diagram below neither kings have castled yet.
In the diagram below the white king has castled king's side or castled short. It has moved two squares to the right and the rook has come to stand on the square immediately next to the king on its opposite side. This way of castling is written O-O.
In the next diagram the white king has castled queen's side or castled long. Again it has moved two squares but this time it has moved to the left and the rook has come to stand on the square immediately next to the king on his opposite side. This way of castling is written O-O-O.
There are certain conditions which prevent either player from castling:
Castling is not possible if either the king or the rook has moved.
In the diagram the king cannot castle queen's side because the rook has moved.
All of the squares between the king and the rook must be empty.
In the diagram the king cannot castle queen's side at present because the knight is between the king and the rook.
If the king is in check it cannot castle out of check.
In the diagram the rook is checking the king. The king cannot castle on this turn but as long as White does not move the king to get out of check it may be possible for White to castle on a later move.
The king cannot castle if doing so would put it in check.
In this diagram the black bishop is attacking the square (the highlighted square) the white king would rest on if White castled on the king's side. Consequently the white king would be in check. However, White can castle on the queen's side and perhaps later he could castle on the king's side.
The king cannot castle if it has to cross a square which is being attacked by an enemy piece.
In this diagram the queen is attacking one of the squares (the highlighted square) the king would have to cross if it castled queen's side so the king cannot castle on this side at the moment.
It is generally a good idea to castle early in the game to get the king to safety before attacking and opening up lines in the centre. Remember - those lines could lead directly to your king and leave it open to attack by your opponent!