So I've got this task where I have to setup a virtual network with an OSPF routing protocol. I firstly ignored this loopback interface I had to configure on the routers since it didn't do any difference in this virtualization software called Cisco Packet Tracer (I thought). Then I've built the network in real life with some Cisco routers and it didn't do anything either. With or without this loopback the network worked (pinging from one host to another). Now my question is: Why is this loopback interface needed or what function does it do? In the picture below is the network I had to build (if it is any help).
The loopback interface is useful because it is an interface with an IP address which never goes down. OSPF, without a specifically defined Router ID, will pick a Router ID on its own. It chooses the Router ID from the IP addresses of the configured and enabled interfaces. A loopback is a good choice since the loopback interface is always up unless someone specifically shuts it down. Other interfaces may go down if there is a problem on the link.
First, loopbacks interfaces are mainly used when we want to establish adjacencies between 2 equipments (i.e. routers) and to be sure that when one link fails, the adjacency will not go down because, loopbacks interfaces are logical interfaces, and you can reach them by different ways.
Another use for this is to announce some networks. Networks can only be anounnced even if the existed in the routing table. I guess, in the above example, when you comment all the loopbacks interfaces, one use that can be done for this, is to announce some networks and see how OSPF can work, but, even if you use or not loopbacks interfaces, your configuration must work fine.
Adding to @Ron Maupin great answer I would further say that the (wise) choice of the router ID to be the loop back interface will be more "powerful" upon link failure scenarios. As others mentioned, every OSPF router chooses a router ID. This ID is chosen among ALL available interfaces on a given router UNLESS explicitly configured otherwise. So, on any link failure for a specific router - if the logic of the router ID selection is still set on the "highest IP address" and there is no loopback address configured as well in the OSPF process (or there is no loopback address in the router at all) - then this link failure will trigger a new router ID selection procedure "within" the router and, perhaps more importantly, will obligate this router to advertise its "newly elected" router ID, meaning send OSPF messages again onto the network.
On the other hand, if the router ID was set "deterministically" by configuring it to be the loopback address (or if there is any loopback address in the OSPF process), that will never go down (unless of course, the entire router/OSPF process will go down), then if any one of the interfaces of the router goes down, the router ID won't be affected, therfor no multicast OSPF "new router ID" messages will be sent onto the network.
Considering the above topology, in case router E (or more precisely its only interface) goes down, then anyway, when it will go up again it will still advertise its router ID "all over again". But (!!) if any other router (A, B, C or D) will have one (or more) of its interface(s) down, then if the router ID was not "set deterministically" - new advertisement will have to be sent onto the network, which will affected the overall bandwidth of it. And this is the case where loopback address for router ID in OSPF is beneficial.