To be honest, I've found for many users it's best to just shutdown their MacBook prior to disconnecting it from a display (especially if it's also drawing its power from the display).
However, you should also be able to just put your Mac to sleep prior to unplugging the display and Apple actually recommends at least doing so:
It's best to put your Mac to sleep by choosing Apple menu > Sleep before disconnecting your display.
Also, only some DVI and Mini DisplayPort displays can be unplugged without putting the computer to sleep, but since you're using an Apple 27" display (which should be fine), you can try resetting the NVRAM to see if this addresses your issue.
Resetting the NVRAM on your model MacBook Pro
Older Macs had what's called Parameter RAM (PRAM), newer Macs use Non-Volatile Random-Access Memory (NVRAM). In your case here’s how to reset it (I recommend doing this using the built-in keyboard and while your MBP is not connected to the display):
- Shut down your machine. Yes, a full shut down, not just logging out.
- Press the power button and then press the commandoptionPR keys. You have to make sure you press these keys before the gray screen appears or it won’t work.
- Hold those keys down until your Mac reboots again and you here the startup chime.
- Let go of the keys and let your Mac reboot normally.
Note: When you log back in you may need to readjust some of your system preferences (e.g. speaker volume, screen resolution, startup disk selection, time zone information, etc).
Resetting the SMC on your model MacBook Pro
If, after resetting the NVRAM, you notice no difference, you can also reset the SMC.
Follow these instructions to reset the SMC on your model MacBook Pro (once again, I recommend doing this using the built-in keyboard and while your MBP is not connected to the display):
- Shut down your computer
- Keep the MagSafe adapter (power cable) plugged in.
- Press at the same time shiftoptioncontrol (on the left side of the keyboard) and the power button
- Let go
- Turn your computer back on with the power button.