I think every religion has some form of this idea: you reap what you sow, karma, cause and effect, etc. I like to think of it in terms of blessing more often than justice for bad behavior. I think most philanthropists would agree that they feel far more blessed in return for what they give than the price they pay. I also agree with the posts that suggest much of it is controlled by an attitude. Responding to negative situations with a positive attitude very often makes the outcome more bearable. The adverse is also true.
The phrase is used to imply that what you put out into the universe will come back to you. If you exude negativity, then negative things will happen to you at school, at work, at home, and just in general. If you did someone wrong in the past, then expect that to come back around and someone will wrong you.
Illustrations of the phrase in practice - think about any crook who has ever been caught and found guilty! S/he planned and plotted and schemed and carried out whatever illegal activity s/he chose, but in the end the effort went for nothing because the criminal was arrested, convicted, and punished for whatever was done.
I am reminded of one of my former principals--a woman--who showed an incredible gender bias against her male teachers. There were different rules for male and female teachers, and men were treated with less respect in virtually every way. Several male teachers, including myself, left the school in part because of her unprofessional behavior, which certainly bordered (if not eclipsed) sexual discrimination and harassment. Just a few months after I left, she was charged with sexual harassment by one of the remaining male teachers, and she "retired" soon after. It was a classic case of what comes around goes around.
I usually heard it as what comes around goes around. It basically means that if you treat people with kindness, you will receive kindness in return. If you treat someone badly, people will begin to treat you badly as well. Remember the Golden Rule!
The expression is often used in the negative to suggest a kind of karma at work in one's life. It means that, quite often, you get what you give. If you bully people, then you could be bullied too. If you gossip about a friend on Facebook, then someone, someday, is going to gossip about you that way. If you cheat on boyfriend or girlfriend, you will be cheated on one day. It is kind of like fate is going to come back and get you at some point.
Try to think about some time in your life when you needed something from someone and then later they needed the same from you. I see this with my kids a lot. The younger one will want something and the older one will say no. And then a while later, the older one will want something and the younger one will say no, maybe in revenge. Thankfully, sometimes the same happens in a positive way. One will share candy with the other and then the other will do the same the next day.