Confusing Junction Box
Divide and conquer

First, tie all the grounds together with an additional 6" pigtail of ground (your next lamp will use this) and push all that into the back of the box.

Next, stick a wire-nut cap, and tape, each of the wires.  You will need at least black and possibly red electrical tape.  

You will also need a non-contact voltage tester, unless you want to get fancy with splicing wires to a lamp socket one at a time.  

Hunt for the supply cable

Turn on the switch which controls this light.   Now use the non-contact tester to sense for voltage on each of the wires.  You should find it on one black wire.   If two black wires are energized, or a white wire, that is trouble. 

Now turn off the switch for this light.  Does the power go away? We need to know that.  

Now turn the switch back on.  Has anything else in the house lost power? 

Also is there another lamp meant to be switched by this same switch? 

Black wire switches on/off with the switch

This one is easy.  The black is a switched-hot.  Mark it with 3-4 wraps of red tape.  Since there's no other source of power, all the other blacks must also be switched-hots. Mark them with red tape also. 

On your new lamp, mark the black (or brown) wire with red tape.  If the lamp has a light blue wire, mark that white (in your mind if you don't have any white tape). 

Nut all the white wires together.   Nut all the red wires together.   DONE! 

Black wire is hot regardless of switch position

First, wrap black tape around the pair of wires as far back as you can, this is to mark them as the always-hot supply cable.

This will need a little more detective work.  One of the two remaining pairs is actually a switch loop.  The two wires are shorted out when the switch is on.  It would have been neat if the last guy had marked it with tape, like he's supposed to, so look for that.    Or if you have an ohmmeter, you can switch the power off, then test which white/black pair has infinity ohms with the switch off, and near zero ohms with the switch on.  

If you have the new lamp, you can first tape its black (or brown) wire red, then attach the lamp's white to the supply's white (these are neutral), then pick a cable and connect its black to supply black and its white to lamp red, making a triangle of wires (with one set still dangling). Re-energize.   

If working the switch causes this lamp to switch from off to full power, you have found your switch loop.  De-energize and  unhook it.  Mark the switch loop's black wire with red tape (switched hot), and its white wire with black tape (always-hot).  Pull the switch and mark the wires on that end the same way. 

If it doesn't work, try the other cable.  If neither one works, I am stumped, it's electrician time.

Now the question of the third cable

If it goes to  another lamp that is meant to be switched with this one, then mark its black wire with red tape.  

If it goes to supply continuous power to something else that is now dead, then leave the black wire black (or tape it black to reflect that you have identified it).  

It has to be one of these two things.  

Finally...

Get some wire nuts and nut all the white wires together.  Nut all the black wires together.  Nut all the red wires together.  DONE!