Not to get all clinical about it, but I think loneliness is probably a survival mechanism. Humans are social animals -- some more social than others. In a daily survival situation, we'd be like a pack, cooperating in a way that ensured the maximum number of people surviving. If we find ourselves alone for a long period of time, it may be that the lack of interaction with others kicks into loneliness, driving us to seek out other people and giving us a better chance of surviving.