In computing, the localhost is the computer against which you are sitting. Literally it means "this computer". The IP 127.0.0.1 refers to the localhost, as a host is any computer or server, so the local host is the one you are using. 127.0.0.1 is the address that points to your PC, from your PC, and is called the Loopback IP address. Loopback refers to data flow routing.
It is a mechanism that the host uses to access its own network services, regardless of network configuration. The loopback can be useful for many things, such as accessing a web service that is installed on the machine itself, by just using the address 127.0.0.1 that points to the same computer.
We can say that IP address 127.0.0.1 is your house, directing your home from home, if that makes any sense.
Almost all standards IPv4 networks use the loopback address 127.0.0.1, so they reserve the entire block of addresses for this purpose. The localhost is always resolved at address 127.0.0.1. In contrast, IPv6 uses the loopback address:: 1 or 0: 0: 0: 0: 0: 0: 0: 1, which for many it makes more sense.
But why 127.0.0.1?
The reason doesn’t seem to be completely documented, but thanks to this thread in superuser.com you can get a much clearer idea.
127 is the last number in a class A network with a subnet mask of 255.0.0.0. And, 127.0.0.1 is the first assignable address in the subnet. The values 0, 127, and 255 are special in 8bit assembly language. 127 in binary is 01111111, and 255 is 11111111. If 0 and 255 are reserved, then the choice of 127 could be the most obvious or the easiest to remember.
This document from 1986 on the allocation of network numbers sheds some more light on the matter. I quote:
“Zero address must be interpreted as meaning ‘this’, as in ‘this network’. For example, the address 0.0.0.37 could be interpreted as the host number 37 on this network.
The address of only a few must be interpreted as meaning ‘all’, as in ‘all hosts’. For example, the address 220.127.116.11 could be interpreted as all hosts on the network 128.9.
That way, number 127 on Class A network has been assigned the "loopback" function, this means that a data stream sent by a higher-level protocol to a network 127 should go back inside the host.
On 1981, 0 and 127 were the only reserved Class A networks. 0 was used to point to localhost, so that left 127 for loopback. However, 127.0.0.1 could soon stop being the localhost as the IPv6 protocol assigned a new address, and when this finishes to fully seize communications, our home will then always be:: 1.