Gallstones can form when there's an imbalance in the substances that make up bile, such as cholesterol or bilirubin. These substances can crystallize and become stones.