It's important not only in terms of physical compatibility but also in terms of performance compatibility.
You don't want to buy an expensive and powerful GPU only to pair it with a weaker CPU. What will happen is your CPU will hinder the performance of your GPU, and thus, you would have spend a lot of cash for bottlenecked performance (which you could have avoided by selecting an ample GPU to match the CPU, or vice-versa).
There is also the usage compatibility (between what you buy and what you intend to do).
You can buy any Z270 motherboard you can afford, but, if you get a powerful i5-7500 (locked CPU) for example, you would have paid for something you can't use (i.e., the motherboard's ability to overclock an unlocked CPU). You can also buy a very expensive i7-6950X for example, but if your main purpose of the PC is purely gaming and you'd never use it for multi-tasking or editing, then you could have spent wisely for an i7-7700K (or even an i5-7600K which is substantially lower in price and better use in gaming.