The mutations are random errors. However, which mutations become fixed sometimes depends on pure chance if they are selectively neutral, meaning they neither increase nor decrease the chance of survival or reproduction.
If a mutation is harmful then it will be eliminated by natural selection in most cases. Most mutations are harmful. Sometimes a mutation happens to be adaptive or advantageous by chance. If so, then it will likely be preserved and may even become more common through time. When the environment changes, then mutations are more likely to be advantageous since the genes that an organism already has are adapted to a different environment, the original one, and are therefore less likely to be superior. But since environmental change is unpredictable, and since it may not happen for a long time, mutations are as a result less likely to be adaptive or advantageous.