We use this phrase when something unplanned or unexpected occurs. When you go to a party, and someone you know just happens to be there, it means that you didn't plan to meet that person at the party, and it was completely unexpected for you to see that person there.
This expression is also used when someone learns a fact that he didn't know before. For example, if you were in the kitchen with someone you barely knew and you found out later that he was a chef at a five-star restaurant, you can later say, "The person I was cooking with just happened to be a chef. I had no idea." As you can see, there's often an element of surprise involved when you use this phrase.
A: I hear you're planning a trip to Miami next month.
B: Yes. I'm looking for a cheap, but nice place to stay.
A: Well, I happen to be from Miami, so I can give you some ideas.
B: Really! That's great!
A: ... and you're going with Sarah, right?
B: Actually, she just happens to be visiting some family there at the same time, so we decided to hang out.
A: Really. That's hilarious.
B: I know. We're going to have a blast.
have a blast - have a lot of fun
Happy Monday, folks. ... and have a nice week.