Domain names know what server to connect to through the use of the DNS or domain name system.
The DNS or domain name system is what acts like a phonebook for the internet, which translates human readable website names like mysite.com into machine readable IP addresses like 175.0.24.24 etc.
When you type the URL also known as the domain name into your browser or click on a URL or domain, a rapid and hierarchical lookup process occurs to be able find the correct web server and load the website that is connected to the URL.
The DNS Process for the domain names starts with a browser cache check, in which your computer and browser checks if it has recently visited the website.
And if your computer and browser or device has already visited the website, then it already knows the IP address of the website.
However if the IP address of the website is not cached in your browser, then your browser will query your ISPs or Internet Server Providers recursive resolver, which then acts as an intermediary search for the IP address of the server that the website is hosted on.
Next the resolver asks for a Root server, which is the top of the hierarchy to find the relevant Top Level Domain or TTLD server, like .com, .net etc.
Then the TLD or Top Level Domain finds the specific authoritative nameserver for that domain.
And the Authoritative Nameserver also holds the official A record or Address record and returns the definitive IP address for the domain name to the resolver.
And finally the resolver then delivers the IP address of the server for the website to your browser, which then connects directly to the web server.
The main and key components that route traffic for the domain to the website and IP address to the server are the A Record or address record that maps the domain to it's IPv4 address and is the most fundamental record.
The AAAA Record, is similar to the AI record, but is for IPv6 addresses.
The Name Servers or NS Record are records that indicate which DNS servers are authorized to provide the definitive mapping for the domain.
And Caching/Time to Live or TTL, which is when the DNS records are cached by your ISP or device for a specific time or TTL to speed up future connections to the website.
If a server happens to host multiple websites, then the web browser also uses HTTP protocol headers to tell the server which specific website the user is looking for, which also ensures that traffic reaches the correct application and not just the correct server.