The ability to imitate sounds is a surprisingly uncommon skill. In fact, very few animal groups have been shown to be able to learn to copy the sounds that they hear. This ability, called vocal production learning, is common is many songbirds, and in parrots, but very rare elsewhere in the animal kingdom. But it's a very important trait to understand, because it's probably an essential component for the evolution of language.
The detailed reasons that some species are capable of vocal production learning are probably quite complex, but in essence it boils down to the fitness advantage that such an ability does or does not provide. In species like songbirds, where territorial defence and mate attraction are often carried out at a distance, there may be an advantage to be able to sing songs that are noticeably different from your neighbour's, but still noticeably belonging to your species (otherwise females wouldn't approach you!). In highly social species such as parrots and dolphins, vocal learning can lead to individually distinctive calls that help mediate social interactions - essentially, names. Or, more controversially, highly intelligent and problem-solving species may use these complex and variable signals to exchange information about the environment and how to respond to it.
But in the end, every evolutionary innovation comes at a cost. A trait only persists if the benefit outweighs that cost. So most animals can't imitate sounds simply because they have no ecological need to do so, and the brain development that would be necessary for that ability simply is too costly to make it spread through the population via natural selection.