IP Addresses deliver a packet to a specific machine. But once the packet arrives, the Operating System must deliver it to the correct application. If you have Spotify, Chrome, and Slack all downloading data simultaneously, the OS needs a way to separate the traffic.
This is where Ports come in. A port is a 16-bit number (from 0 to 65535) that acts as a logical "door" on the machine. Servers listen on "Well-Known" ports: HTTP uses port 80, and HTTPS uses port 443.
A single connection between two applications is uniquely identified by four pieces of information, known as the 4-Tuple:
If you open three tabs in Chrome to the same website, the Destination IP and Destination Port (443) are identical for all three. How does the OS know which tab gets which data? Because of the Source Port.
When an application makes an outgoing connection, it doesn't usually care which port it sends data from. The OS automatically assigns it a random, unused high-numbered port called an Ephemeral Port.
Click to open new connections from your browser to Google's HTTPS server. Notice how the OS assigns a unique Source Port to keep each connection distinct.
Because the ephemeral port is unique, the server knows exactly where to send the response, and your OS knows exactly which browser tab to route the incoming packet to.
This raises an interesting scaling limit: since there are only 65,535 ports available, a single IP address can only have about 65,000 simultaneous outgoing connections to the exact same Destination IP and Port. This is called Port Exhaustion, and it frequently brings down large-scale load balancers.