Typically, the mixture of "compound A" and "impurity B" is dissolved in the smallest amount of hot solvent to fully dissolve the mixture, thus making a saturated solution. The solution is then allowed to cool. As the solution cools the solubility of compounds in the solution drops. This results in the desired compound dropping (recrystallizing) from the solution. The slower the rate of cooling, the bigger the crystals form.
In an ideal situation the solubility product of the impurity, B, is not exceeded at any temperature. In that case, the solid crystals will consist of pure A and all the impurities will remain in the solution. The solid crystals are collected by filtration and the filtrate is discarded. If the solubility product of the impurity is exceeded, some of the impurities will co-precipitate. However, because of the relatively low concentration of the impurity, its concentration in the precipitated crystals will be less than its concentration in the original solid. Repeated recrystallization will result in an even purer crystalline precipitate. The purity is checked after each recrystallization by measuring the melting point, since impurities lower the melting point. NMR spectroscopy can also be used to check the level of impurity. Repeated recrystallization results in some loss of material because of the non-zero solubility of compound A.
The crystallization process requires an initiation step, such as the addition of a "seed" crystal. In the laboratory, a minuscule fragment of glass, produced by scratching the side of the glass recrystallization vessel, may provide the nucleus on which crystals may grow. 
Successful recrystallization depends on finding the right solvent. This is usually a combination of prediction/experience and trial/error. The compounds must be more soluble at higher temperatures than at lower temperatures. Any insoluble impurity is removed by the technique of hot filtration.