If your foam mattress is new and smells strongly of chemicals, then it's off-gassing, a normal release of fumes and volatile organic chemicals into the air. If your foam mattress is older and smells earthy or musty, the problem is mold or mildew, probably growing on the underside of the mattress.
Airing out a new mattress will get rid of the scent of off-gassing. Instead of putting a new mattress on your bed frame, leave it on the porch or in an unused room with windows open for a few days to a week. For mold smells, check the bottom of the mattress. Remove any visible mold, then wash the bottom of the mattress with a sponge dampened with a solution of 1/2 cup bleach in a gallon of water.
You can purchase an organic mattress made from cotton, wool or natural latex to avoid exposure to the chemicals found in new foam mattresses. If you have mildew problems with your mattress, improving air circulation around it will prevent them from returning. Run a fan or a dehumidifier in the room. A slat base or a box spring beneath your foam mattress will also improve air circulation.