It's easier to look at it from the perspective of why nitrogen cannot form more than 4 covalent bonds (after all, the octet rule is more like an exception that only applies to a few atoms instead of a rule).
First, all atoms "have" all possible types of orbitals (an orbital is nothing more than a probability function for the location of an electron with a certain amount of energy). But most of these orbitals are unfilled, and are much higher in energy than the filled orbitals of that atom. So, look at nitrogen. Nitrogen has a filled 1s and 2s orbital, and partially filled 2p orbitals. It also has completely empty 3s orbitals, 3p orbitals, 4s orbitals, 3d orbitals, etc (even f orbitals). In the case of nitrogen, these empty orbitals, even the 3s and 3p orbitals, are much higher in energy than the 2s and 2p orbitals, so these orbitals can't be used in bonding. The orbitals are still there, but they just require too much energy to fill. So nitrogen is stuck making bonds with only 2s and 2p orbitals, and since that is a total of 4 orbitals which each hold 2 electrons, you can make a maximum of 4 bonds to nitrogen.
Phosphorus has filled 1s, 2s, 2p, and 3s orbitals, and partially filled 3p orbitals. Just as with nitrogen, it also has all other orbitals, but they are unfilled. So that means it has 3d orbitals. In the case of phosphorus, the 3d orbitals aren't that much higher in energy than the 3p orbital. The smaller energy difference means that the 3d orbitals can be used in bonding. This enables phosphorus to go hypervalent, even though elemental phosphorus has completely unfilled 3d orbitals.