The foam itself is not an automatic sign that you need a new roof immediately. Moss grows naturally on or between shingles, especially on roofs in shady or humid climates. But the foam could allude to hidden damage.
The problem with the foam is that when it rains, the foam absorbs the water like a sponge and holds it back. The purpose of the shingle is to allow the water to roll from the roof to the gutters. The presence of foam prevents moisture from getting where it is supposed to go. This can lead to the destruction of the shingles. Since the foam prevents water from reaching the gutters, it can also seep into the layers under the shingles, causing water damage and mold growth.
Therefore, if you see foam, you will need a roofing contractor to clean it and look for signs of permanent damage.