Foamy sperm can be caused by dehydration, infrequent ejaculation, retrograde ejaculation, or underlying medical conditions.