Mutations are random. Different mutations gives different traits. Some traits are good for animal, some are bad. Those with good traits have slightly higher chances to breed, but that's good enough for long-term changes.
Imagine hypothetical situation. We have 100 giraffess and trees. For some reasons trees became slightly taller, so giraffes with mutation, that gives longer neck will have advantage. Let it be divided like that: there is 10 tall giraffes with 100% chances to breed, 80 normal giraffes with 50% chance and 10 short giraffes with 20% chance for having successful children with their genes. Let's imagine that each couple only have 2 children. After 1 generation there is still 10 tall giraffes, but only 2 short giraffes and 40 normal. After 1 more generation there is 10 tall giraffes, no short and 20 normal, after 1 more generation, amount of tall and normal giraffes is equal, but then number of normal giraffes falls and falls until almost all giraffes are tall enough.