So viruses normally mutate every time they infect a host and force it to replicate them. This is called antigenic drift. These mutated forms are slightly different, so our immune system might not completely recognize them, but there is usually partial recognition, and hence partial immunity.
But if 2 viruses infect a host simultaneously, they can essentially combine to form a radically different virus. This is called antigenic shift. This new virus is often so completely different from anything seen prior that no one has much immunity to it. This seems to be the starting point of many of these pandemics.
https://www.cdc.gov/flu/about/viruses/change.htm