Surface tension. When the urine stream hits the water, it splashes a little, and air gets mixed in. When the air comes up, it might not have enough force to overcome the surface tension of the water, and it forms a bubble.
But urine isn't pure water. Pure water tends not to bubble as much.
Not a scientist or a doctor but pee which foams up can be an indication of high protein in your urine. Can be nothing or point to some underlying issues. Let your doctor know so they can run some tests.
If it's only slightly foamy and not all the time it's fine. Especially for guys if you've ejaculated recently. But if it's real foamy frequently, yeah might want to check it out. Or if it's particularly discolored or smelly that you can't attribute to your diet.
Also fun fact, apparently if you go running with a completely empty bladder, you can bruise it, and you get reddish-brown, foul-smelling urine from some of the dead cells being purged. It's a little terrifying to see, ask me how I know.