Dogs, as many other animals, have primaeval fear of thunder. They will instinctively react by hiding since they know that the next thing that follows is rain, storm, flood, earthquake, volcano eruption...
Firecrackers, fireworks, gunshots and other unexpected noises – especially explosive/detonating ones – can cause fear. One of the reasons is they are reminding of thunders and other natural explosions, and come all of a sudden. The thing that makes dogs react even more is their extremely sensitive sense of hearing. If a human gets startled, a dog can experience it as a huge shock. It will not know what to do so it will get into panic state, running left and right or straight ahead overwhelmed by panic.