Genes do not know to adopt or evolve. It is a naturally occurring process that happens by chance when a base is misread, so from an A to a G for example. This process of adaption has positive outcomes allowing species to adapt to their environments and therefore has been maintained since life began
As David said the genes dont “know” they are just affected by the natural process of evolution. When genomes are copied (when the organism reproduces) mistakes sometimes occur. so the letter of the DNA code may change or entire henes may be copied or even lost. The copying machinery for DNA is very accurate so the mistakes are very rare in bacteria for example it can be 1 mistake every million letters of DNA, but when mistakes do happen they can potentially change the function of a gene they encode. Some mistakes allow the organism to become better suited to particular environments i.e. to adapt and this process is known as adaptive evolution.
I think you can imagine evolution to be like a river taking carrying a stick down it, the stick is being carried along by the river but it doesnt know it has to move along the river. In this case the stick is like a gene.
Comments
Stéphane commented on :
Natural selection is the principle from which if you have the genes then you will adapt. If not, the individual gets extinct.
There is this great article about the fact that humans are finished with evolution.
https://www.the-scientist.com/?articles.view/articleNo/46651/title/Humans-Never-Stopped-Evolving/
Stéphane commented on :
Human are not* finished with evolution !!! (Sorry typo)