Well, that is simple.
A Carbon (C) makes 4 bonds, a Hydrogen (H) or a halogen (X) makes 1 bond and Nitrogen (N) makes three bonds. So in a molecule with C$c$ Carbons, H$h$ Hydrogens, X Halogens$x$ halogens and N$n$ Nitrogens, we will have $\small\ce{4*C +3*N + X +H}\ \ \ $$4c+h+x+3n$ "bonding capacity".
Assuming there are no rings, no double/triple bonds, we must have $\small\ce{C +H + X +N -1}$$c+h+x+n-1$ bonds in between these atoms [think like how many dashes you can draw between $n$$m$ $\ce{O}$'s: $\ce{O-O-O}$ two dashes for three $\ce{O}$'s apparently].
Since each bond uses one "bonding capacity" from each of the two atoms its binding, $\small\ce{C +H +X +N -1}$$c+h+x+n-1$ bonds are using $\small\ce{2*(C +H +X +N-1)}$$2(c+h+x+n-1)$ bonding capacity. So the remaining capacity is:
$$\ce{(4*C +3*N +X +H)\ -\ 2*(C +H +X +N - 1)\ =\ 2*C-H-X +N +2}$$$$(4c+h+x+3n)-2(c+h+x+n-1)=2c-h-x+n+2$$
If this is larger than zero, it means our molecule is unsaturated, i.e. still has a capacity to make bonds. And our assumption that "there are no rings, no double/triple bonds" is wrong. This bonding capacity is used in forming new bonds either in rings or in pi-bonds. Since each bond uses two capacity one from each side we should have $\small\ce{C-H/2-X/2+N/2+1}$$c-h/2-x/2+n/2+1$ "unsaturated" bonds.