This occurs when rain water dissolves chemicals from the treebark as it flows down the stem of the tree. This changes the surface tension of the water, so that when it drips down towards the base of the tree, air is introduced due to the turbulence of the water, and foam is formed because of the altered surface tension.
Eucalyptus bark and leaves contain glycosylated alkaloids or isoprenoids called saponins, which foam when wet, they don’t emerge from the tree but simply wash off the leaves and bark.
I spend a lot of time in forests but I have never seen this occur. Perhaps it only happens after extended dry periods (or perhaps it often happens and I am not observant enough). Just wondering if anyone else has seen foaming trees?