Igneous rocks form as a result of magma crystallization (freezing) as the temperature of the magma decreases.
You might expect that the types of minerals that crystallize depend only on the original composition of the magma, however, rock formation is more complex.
Different minerals have different melting or freezing points and so as the temperature of magma decreases, minerals with the highest melting point crystallize first. Extracting these minerals from the melt, alters the magma composition.
The remaining magma may have different physical characteristics such as density, and thus may be more buoyant than the original melt.
An increase in buoyancy may allow the remaining liquid to separate from the already formed crystals, leading to chemical/mineral differentiation in a process called fractional crystallization.
Sometimes crystals can react with the changing melt composition changing for example, olivine to pyroxene. The sequence of minerals crystallizing from a melt is referred to as Bowen's Reaction Series.
On a large scale, differentiation within Earth is evident in the separation of iron into the core and the lighter elements into the crust.
When magma cools, it is surrounded by cooler rock. Frequently, pieces of this surrounding "country" rock are assimilated into the magma, altering the chemical signature of the rock.
Often pieces of the rock surrounding the magma can become entrained in the liquid. After cooling, these foreign pieces of rock are surrounded by the solidified igneous rock. We call these fragments in igneous rocks xenoliths.