Let me answer with an analogy each car has a unique number that identifies it from it's manufacturer, the company has a list of all the cars that where send to each mayor distributor on each part of the world, each one of those distributors has several dealers to which they assign a set of cars to sell, and each one of those dealers sells the cars to end customers. So in theory if the manufacturer wants to know where is the world is a car he doesn't has to ask because he know in which country it landed.
Translating that to IPs every company that sells public IP address has a record of who owns it, and they are normally give them away in bulks of 1000s to ISP (phone numbers used to be like this). For example I can tell you if an IP is from my country just by looking at the first 2 groups. On the other hand hosting providers and data centers work the same way and they almost always know where is the machine physically, and last but not least doing a trace will jump hops to the closest (theoretically, as you can force the traces to be what you want) IP to the box which means you can guess the location if you have the one of the hop before it.