When you shake an empty tree, a beehive might fall out. The angry swarm of bees will then fly toward you and, if you don't catch them, they will sting you. This page describes what to do about bees.
Get a Bug Net
The bug net is the only way to catch bees. You can buy one at the Nooklings' store.
Catching Bees
When you shake an empty tree, listen for the sound of a falling beehive. Your character will react with shock. The bees will fly around the nest for a moment, then they will start chasing you.
However, you can try to avoid this by equipping your net first. If you are close enough to the tree, you should be able to shake the tree even though you are holding the net. Then, if a beehive falls, your character will turn toward it, so all you have to do is swing the net, and you will catch the bees before they can come out and chase you.
If the bees do start chasing you, you can open your inventory and the bees will be "paused," and they won't be able to move closer to you until you exit the inventory. Therefore, to increase the chance that you successfully catch the bees, run north from the beehive as soon as it has fallen out of the tree, then go into your inventory and equip the bug net. When you equip the net, you automatically face south, and since you ran northward, you are now facing the direction that the bees will be coming from.
When you are ready, exit your inventory. As soon as you can see the bees on the screen, use your net. If you timed it correctly, you will catch the bees.
Be sure to collect the beehive, too. You can sell it!
Running Away
If you don't think you can catch the bees, try to go into a nearby building. The bees will go away, so you can go back outside and those bees won't be chasing you anymore.
Curing Bee Stings
If you aren't able to avoid the bees, they will sting you if they catch you. If this happens, your eye will be swollen shut, and villagers will comment on your appearance, unless you wear funny glasses or other eye-covering accessories to hide your swollen eye.
To cure your swollen eye, you can use medicine that you purchase from the Nooklings' store, or you can simply save and quit the game. When you start again, your bee sting will be gone.
In Other Villages
If you are visiting another player's village, you will not find beehives in trees. You can shake all of the trees in the other person's town without fear of finding bees.