When asking why a particular group of organisms, such as mammals, has a particular feature, such as a long snout, you must be careful about the hypothesized reasons. While the other answers here propose various adaptive reasons, they do not actually explain the presence of the long snout in the majority of mammals.
Instead, you have to look at the evolutionary history of mammals and vertebrates. Modern mammals have long snouts because they inherited the trait from their earliest mammalian ancestors, such as Morganocodon. In turn Morganucodon and other early mammals had long snouts inherited from their synapsid ("reptilian") ancestors like the sphenacodontid (see the first phylogenetic tree below). Why did the synapsids have long snouts, then? Because the earliest tetrapods like Ichthyostega had snouts. Before that, the lobe-finned fishes like Eustenopteron and Panderichthys had the elongated snouts (see the second phylogenetic tree below).
Of course, the mammalian snouts were modified through natural selection to take on various shapes and sizes (or become greatly reduced) for various (presumably) adaptive reasons, as suggested by the other answers.
An analogous question is why do all mammals have four appendages (arms and legs)? They inherited the four appendages from their common tetrapod ancestor, which in turn inherited the appendages from their ancestral lobe-finned fishes. The appendages are later modified by natural selection. That same reasoning applies to this question about mammalian snouts.
When using phylogenetic trees to study the adaptive changes of particular traits (such as snouts), a researcher can first use comparative techniques like phylogenetically independent contrasts to tease out the effects of inheritance due to common ancestry. See for example Felsenstein, J. (1985) and the many, many papers that cite this one.
Felsenstein, J. 1985. Phylogenies and the comparative method. American Naturalist 125: 1-15.
Eusthenopteron to Pederpes phylogeny modified from Maija Karala, Wikimedia Commons (CC-BY-SA-3.0).
sphenacodontid by dmitrchel, Wikimedia Commons (public domain).
Morganucodon by Michael B.H., Wikimedia Commons (CC-BY-SA-3.0).
Ichthyostega by ArthurWeasley, Wikimedia Commons (CC-BY-2.5)
Tiktaalik by Nobu Tamura, Wikimedia Commons (CC-BY-2.5).