There is natural variation in the lengths of giraffes' necks. As there will be competition for food with other giraffes and other species, those with longer necks would reach leaves on the trees which others can't. As a result they are more likely to be able to get enough food and are more likely to survive over those with shorter necks. Because the ones with longer necks may be more likely to survive, they are more likely to be able to reproduce, passing the alleles for a longer neck on to their offspring. Over many generations, the frequency of the long-neck allele will increase, and so they have evolved to have long necks by natural selection.