How does a computer (or USB device) detect that there is a new/lost connection?
What you're looking for is called a "KVM switch" (keyboard, video and mouse), you just don't need the video part. These are commonly available and easy to find once you know the correct term.

But to your specific question about USB enumeration: Every USB device has a pullup resistor on one of the Data lines (one for low-speed devices, the other for all others). A USB host or hub recognizes the presence of this resistor and initiates enumeration when it appears.