Getting your king to safety is super important in chess, and the best way to do so is to castle to one of the sides of the board. This is one of the special moves in the game, and it comes with its own set of rules. Here’s how it works.
Castling in chess is when you move your king and rook at the same time, with your king jumping over a square to land on the side of the board. This is a great way to evacuate His Majesty from the center of the board where the action is likely to heat up.
You can only castle if neither your king nor the rook in question has moved yet in the game. The squares between the king and the rook need to be clear (so get that knight and bishop out of the way.) Also, the king cannot be in check and the square it jumps over (d1 or f1) also cannot be under attack by an enemy piece.
You can castle to the kingside, jumping to the g1 square, or queenside, jumping to the c1 square. In both cases, the rook lands on the other side of the king, on the square it hopped over. This, coupled with the pawns in front (which are hopefully still in place) form a super-strong protective barrier in front of your most important piece.
The same castling rules apply in Fischer Random chess, a variant also known as chess960. It can, however, be a bit more difficult to parse the situation in that mode, so much so that sometimes it even bedevils grandmasters.