Why would adding a USB extension cord to my keyboard affect my Bluetooth connection to my magic mouse?
Are you plugging the keyboard into the USB 3.0 port? The signaling rate of USB 3.0 can cause RF emissions that can interfere with Bluetooth, WiFi, and other devices. Intel has a detailed paper on USB 3.0* Radio Frequency Interference on 2.4 GHz Devices

My suspicion is that the extension cable has better shielding than the keyboard's cable and moving the keyboard's cable just a little further from the chassis drops the RF interference enough for the pairing to work.

Try switching it to the USB 2.0 port. Does the problem go away?

Update: I struck out parts that don't apply to that model. I have the mid-2012 15" Retina which still had the USB 3 on the left and USB 2 on the right.