This is a question that many children will ask their parents at some point, and if the child is chemically literate this is what the parents could tell them. Flamingos primarily eat crustaceans and algae from their aquatic environment, which we usually see them standing in, and in their food, there is what is known as
carotenoid pigments. Carotenoids are made from fats, and the incorporated into the chloroplasts of algae to serve as light absorbing pigment. Then the algae get eaten by the crustaceans or flamingos, the carotenoids get passed on into these animals. Usually when a molecule is eaten, it breaks down, but something such as a pigment will absorb into your bloodstream and not break down, so when a flamingo eats these, the pigments enter the blood stream and then eventually come out in their feathers. Now for flamingos, the primary pigments which get absorbed and end up in their feathers, are
Canthaxanthin (the molecule below), astaxanthin and phoenicoxanthin
1. Canthaxanthin is a molecule which gives of a bright violet colour when it is crystallized but when it is incorporated into flamingo feathers, it is diluted to give a vibrant pink.
References:
1. Fox, D., and T. Hopkins. "Comparative Metabolic Fractionation of Carotenoids in Three Flamingo Species."
Comparative Biochemistry and Physiology 17.3 (1966): 841-56.