How exactly does Cmd-shift-4+space work?
WindowServer

The unit being captured is a layer or window as determined by macOS's WindowServer process.

As a developer, this is accessed through CoreGraphic's CGWindowListCreateImage method:

CGImageRef CGWindowListCreateImage(CGRect screenBounds, CGWindowListOption listOption, CGWindowID windowID, CGWindowImageOption imageOption);


This call returns a composite image based on a dynamically generated list of windows.

Screen Captures with Layers

Tools like Acorn, ScreenShot PSD, and Ondesoft ScreenCapture can take screenshots capturing all the individual layers or windows. The resulting capture retains the layering, allowing you to select, hide, or edit windows as desired.