The f7 pawn is really only a concern for beginners. It is not easy to attack for white with many pieces and is easily defended.
Generally (in the opening/middlegame) you want to surround your king with pieces or pawns for protection. Usually you don't want to bind your pieces to the protection of the king as they have other (attacking) duties. This leaves you with hiding the king behind pawns.
You want those protective pawns to be difficult to attack and difficult to exchange for other pawns as this would open files and diagonals, leaving your king without protection. A typical motif in chess (particularly with opposite side castling) is a pawn storm where the attacking side pushes its pawns forward in order to open lines around the enemy king. Obviously this is most difficult to achieve if the pawns are far away, i.e. close to their original position.
Another way to attack the protective pawns would be with pieces. This is often easier to do in the middle than towards the border where there are fewer squares available for the attacking pieces.
Now in the opening both sides fight for control of the center; basically in order to gain space to operate the pieces and because pieces can easily move to either side of the board from the center. In order to achieve this central control the players typically move one or two central pawns and concentrate piece activity around the center. This means that often the situation in the center is very dynamic and lines can be opened, pieces exchanged... (another typical motif: ''open lines in the center against an uncastled king''). You don't want to find your king among all this action in the middle of the board.
Taking all of this together in many cases hiding the king behind pawn chains on the side of the board after castling is usually a good idea for king safety. Another added advantage is that castling joins the rooks, i.e. the castled rook can easily get to the central files (c-d-e-f) where there are often open or half open files to occupy.
There are also cases where players don't chose to castle (e.g. in some Sicilian and French opening lines), usually in positions with static, blocked centers and easily to attack king-/queen-sides.