As a black employee I'm very plugged in the employee resource group for black employees at my organization. I've been able to give back to the local community through volunteering, develop mentee and mentor relationships through this network and develop a sense that there are folks who are not connected to my day to day work that 'have my back', it definitely helps with retention.
A coworker of mine is very involved with the Indian ERG, and another sings choir with the Christian ERG on Wednesdays.
These sorts of groups get a lot of flak from folks who don't think these groups should exist or that companies should not focus on "diversity", but at the end of the day they provide an infrastructure that communities can build on, and that's very important.
Even when I'm having a terrible day, walking through the hallway and having a quick chat with someone who've I've met through these networks is extremely valuable.
I'd recommend anyone who feels loneliness at work to take a look at what ERGs your company may have established, not all of them are tied to a racial, religious, or sexual identity. The maker group at my employer is pretty big and active last I checked.