If you haven't yet moved your king and rook, you have the option of making a special move called castling. Because the king is such a valuable piece, we often want to hide him away in the corner where the opponent can't get at him. We also want to bring our rooks into play from the corner. The beauty of castling is that it allows us to accomplish both these goals in a single move, but it is only possible to castle once per game.
First, let's divide the board vertically into two halves. In chess, we call the two halves of the board the Kingside, which is the half of the board where the king starts, and the Queenside, which is the half of the board where the queen starts. See the diagram below for an illustration of this:
You can castle on the kingside (also known as short castling) or the queenside (also known as long castling), but the king and the rook on the side you want to castle must both be on their starting square, and the squares between them must all be empty. If you have moved your king, you cannot castle, even if you move him back to his starting square. The same goes for the rooks.
To castle, you first move your king two squares towards the rook. Then you jump the rook to the square on the other side of the king. It's the only time in a game of chess that a piece other than the knight is allowed to jump, and the only time that you are allowed to move two pieces at once. Always move the king first and the rook second, or move both at once. Don't move the rook first.
Use the arrows on the board below to see castling in action. White castles on the kingside, then black castles on the queenside.
Now try it yourself on the board below - drag the white king two spaces towards the rook to castle kingside:
Now try it again, but this time castle queenside:
There are some conditions which prevent a player from being able to castle. As mentioned previously, you cannot castle if you have moved your king (even if you move him back to his starting square), and you cannot castle if you have moved your rook. On the board below, white cannot castle to the kingside, even if he puts his rook back in the corner.
You cannot castle if there are pieces between the king and rook. On the board below, white cannot castle to either side.
You cannot castle if your king is in check. If you are in check, you cannot get out of check by castling. However, if you can get out of check without moving your king (such as by blocking or by capturing the checking piece), then you can still castle later. Some people think that you have to forfeit castling rights if you have previously been in check, but this is not true so long as your king remains unmoved on his starting square. On the board below, black is currently in check so cannot castle.
You cannot castle if, at the end of castling, your king would be in check. You also cannot castle if the square that you would need to move your king across is threatened by an enemy piece - that is, if your king would be in check if he stood on that square. In the diagram below, the white king cannot castle queenside, because he would end up in check from the black knight, and he cannot castle kingside because he would have to cross a square threatened by the black rook.
For more information, why not try one of the suggested reading options below?