It doesn't know, it's just that the mutations that convey greater transmissibility and lower mortality cause those particular viruses to be spread far and wide and therefore be more successful at propagating themselves. That's why Ebola, for example, tends to be rapidly self-limiting. It kills quickly/frequently and is (somewhat) harder to spread. You can't give someone Ebola by walking by and sneezing. Once you isolate the cluster of cases, it quickly fizzles out.