What actually happens when we set direction on GPIOs in controllers ?
NXP doesn't seem to provide this, but STM32F4 user manual (page 270) shows a block diagram of a typical GPIO pin.

As you can see, the pins are connected to all blocks: Analog/Digital Input as well as the Output sections. The control registers enable/disable and configure these blocks so they don't interfere with each other. There are also separate enable signals for internal pull-ups/pull-downs.

The exact details of how this is implemented in silicon, obviously, differs between manufacturers, architectures and processes.