While both gray iron and ductile iron contain graphite, the shape of the graphite differs. Gray iron has a microstructure of graphite flakes, which makes it brittle. Ductile iron, on the other hand, has spherical nodes of graphite, which give it greater tensile strength.
You can see the difference in tensile strength between the two types of iron by looking at Young's modulus of elasticity -- a gauge of stiffness. For gray iron, it is 13.4 by 10^6 psi, and for ductile iron, it is significantly higher, at 24.0 by 10^6 psi.
When tensile strength is not as important, engineers use gray iron because it is cheaper than ductile iron. These applications include internal combustion engine cylinder blocks, pump housings, valve bodies, electrical boxes and decorative castings.
Ductile iron is used to make pipe for water and sewer lines, because it is strong -- but not brittle like iron -- and easy to put threads on so that parts can screw together. Ductile iron is also used in agricultural machinery and automotive parts.