How does PCIe lane allocation work?

Say I have a CPU and motherboard that can support 16 lanes, but I try to plug in say a 10G network card that wants x8 and 2 GPUs that also want x16 each. That would total to 40, but I only have 16 lanes. How exactly would the CPU distribute lanes? Would it prioritize certain devices? Do the devices in their startup negotiation tell the CPU their minimum number of lanes? Could someone give an in-depth breakdown of how exactly that negotiation goes down and what happens when more lanes are needed than available?
Thanks