protecting the king
You need to understand why checkmates happen in the opening and in the middlegame. Typically pieces are lined up like this in early checkmates.
Of course, in this case the checkmate works because two pieces are aiming at the f7 pawn at the same time. If the queen captures the f7 pawn, that queen is protected by the guarding c4 bishop. Knowing basic checkmate patterns ('batteries', smother etc) helps to evade the blundering of an instant checkmate AND attempt to launch one of your own as well.