A few things come to mind here:
First, good on you for wanting to help this guy out. We need more people like you in the world.
Next, HN comments are one thing, but experienced and professional advice is quite another. I would recommend getting an appointment with an experienced psychologist to sit down with them and talk about how to best approach this situation. You want to keep the guy around because he does great work, and you want to help him so he can CONTINUE doing great work.
Now, I hate to say this, but you need to consider the downside and potential outcomes of outright firing this guy as some comments have insinuated. If he's really mentally ill, he could react violently. I don't want to scare you or anybody else here, but let's not be stupid - people with mental illness are more likely to react in violent ways to adversity, especially when forced on them suddenly. Be really careful if you decide this is your best option, and maybe discuss how to best keep your office and employees safe with law enforcement. I'd hate to see an active shooter situation come out of this, but it's a possibility you'd be foolish not to prepare for.
State resources might also be an option for you if you're in a state that _has_ resources for these things, like California. I'm in Texas, for example, and we have next to jack shit for resources for dealing with this sort of thing. So I recommend not really relying on state resources as they can be flaky or non existent.
If a psychologist agrees, maybe - I stress maybe - show him some of these HN comments. If he's a rational person, which he must be at some level otherwise he wouldn't be able to work as well as you say he does, he'll see the awkward position you're in and maybe realize "oh shit, I'm causing problems, I need to make some changes". That may be the first major hurdle in rectifying this situation, and once that realization is in place, he might be more receptive to getting counseling, psychiatric care, and moving into his own place.
It's also worth considering that maybe he's not _able_ to find his own place. Landlords are bastards, and the probability they'd rent to somebody who's ever been homeless is pretty low. He might want his own place but not be able to get anyone to lease to him, and he might be too proud/"professional" to mention this to you. In that case, maybe offering to help him by co-signing the lease (a risk, I know) might move the ball down the field toward a better situation.
No matter how you approach this, I suggest a slow, gradual movement in the right direction, toward getting him some help. Straight up slapping down a "hey dude, this is fucked, get your shit together" kind of approach is likely backfire in a big way and make the problem worse, and outright firing him is risky.
I don't envy your situation, but I do hope it works out well for everyone involved!
PS - a thought that occurred after I wrote this: maybe offer everyone a rotating work-from-home schedule that would minimize (not eliminate) their time in the office and their time around this problem employee. That might make them feel safer, and make things easier on him too. That might be a reasonable stop-gap while you gradually work the problem.
GOOD LUCK!