The term "localhost" is used to refer the same local computer on which the TCP/IP protocol stack is running on. The term "localhost" is mapped internally to the loopback IP address 127.0.0.1 in most of the TCP/IP implementations.
Please visit following link to learn about IPv4 loopback addresses before continuing on this lesson.
When any program/protocol sends data from a computer with any IPv4 loopback address as the destination address, the TCP/IP protocol stack on that computer process the traffic within itself without sending it to the network.
Refer below image, of a ping made to "localhost". You can see that the reply for ping command is coming back from loopback address , 127.0.0.1
When setting up a web server or any other type of server, you can use the term "localhost" to point to the same machine as the server. Since the term "localhost" maps to loopback address 127.0.0.1, you will get the response from the same local computer where the server is running.
I have a web server installed and running on this machine. See below image where I am using to get response from the same local computer, by using the term "localhost" (marked in browser’s address bar).