It's all to do with arbitration. Any system which requires multiple devices to be connected needs some way of determining who should talk when. There are different schemes as you would expect depending on the application.
A common example - in networking we have many nodes all talking to each other. This is done by each node having an address (e.g. IP address), and when a node wants to talk to another node, it sends out a packet to that address. You then have devices such as routers which take packets coming in on multiple ports and forward them on to the correct port. The arbitration is done using memory to store packets until the destination port is free.
Now on to USB. This is actually much simpler than networking because not all nodes are made equal. You have two sorts, a host, and an endpoint. There is only ever one host, but can be many endpoints. In this case arbitration is much easier because only the host port is allowed to talk at will. Endpoints are only allowed to talk when asked to by the host, and the host only ever talks to one endpoint at a time.
For host->endpoint packets, the USB hubs simply pass the request from the host to all of the endpoints. Because all endpoints have an address, only the one to which the request was addressed will do anything with it (e.g. respond), all others will ignore the packet.
For endpoint->host packets, the host first sends a packet to a specific endpoint by address to say "you can talk now", and then that endpoint must immediately send a response. Because only one endpoint is allowed to talk at any given time, the USB hub will simply route the packet from whichever port responds to a request from the host.
In terms of how the host works out what devices are attached, and how endpoint get their address, this is achieved through enumeration.
All host and hub ports have pull-down resistors (15kOhm) on the D+ and D- lines. These put the data lines of that port into a known state when there is no device attached, a state in which the port will not send any data over D+/D- lines at all.
When a device is attached, it makes itself known by connecting either the D+ (full-speed) or D- (low speed) data line to VCC using a 1.5kOhm resistor. This triggers an enumeration event. The port will then begin the process of configuring the device and assigning an address. If you were to plug in two devices simultaneously, they will be enumerated one at a time.
If there are no hubs, the host simply talks to the new device and sets it up. If there are hubs in the system, it is the hub which reports the new device is attached. If a hub reports a new device is connected, the host will instruct the hub to reset the new device and start up communications. During the reset, the endpoint is given a default address of 0 (*). The host can then talk to the endpoint using the default address, and configure it with a unique non-zero address that will allow it to know when it is being talked to.
(*) Because only one device is ever enumerated at a time, the address 0 will always be unique to the newly attached device.
You might then ask, "well how can I then have multiple devices all talking at the same time?". Say you have a mouse, a keyboard, and a flash drive all connected to the same USB hub. We all know you can use your mouse and keyboard at the same time while also copying files to/from your flash drive, but if only one device can talk at a time, how can that be possible?
Well, it all comes down to the fact that the few hundred milliseconds it takes for your brain to notice that you have pressed a key and expect the screen to update is an eternity to the computer. A USB 2.0 interface can run at up to 480Mbps (USB 3.1 can run at up to 10Gbps!), which means that even though the host is only ever talking to one endpoint at any given time, it cycles between them so fast that you can't tell it's doing it.
USB Host: "Hey, mouse on port 1, tell me if you've moved. Ok now keyboard on port 2 have you got any key presses to report? Now you there on port 3, flash drive, store this data for me. Anyone else I need to talk to? nope, ok then, mouse on port 1, tell me if you've moved..."
Human: "Oh look, the computer noticed I just moved my mouse, pressed a key on my keyboard, and copied a picture to the flash drive, all at the same time!"
The host device keeps track of which endpoint addresses are used and will send packets to each one sequentially or as needed (i.e. when the OS request access to a specific device). So while it is not all happening simultaneously, the arbitration is so fast that the computers pet human can't tell the difference.