Comes from the formal meaning of "hang out": to protrude or stick out. Hence, the use of "out" for public. Not to be confused with "hang with", which is an umbrella term that includes socializing publicly or privately.
Friend: "Dude, you never hang out with me anymore."
You: "Not true: even though I don't get out of my house much, I still socialize with you on Facebook, which can be seen by other people."