Gallstones can form when there's an imbalance in the substances that make up bile, such as too much cholesterol or bilirubin. Factors like obesity, diet, and genetics also play a significant role in their development.