How can some animals see ultraviolet or infrared light?
Short answer
In mammals dedicated UV cones have been found, as well as photoreceptors with secondary peak-sensitivity in the UV range. In fact, human blue cones are sensitive to near-UV.

Background
In humans, the visible spectrum is generally accepted to range from 390 to 700 nm. Figure 1 shows the spectral sensitivities of the various photoreceptors in humans.


Fig. 1. Spectral sensitivity of the four receptor classes. Source: Wikibooks; Sensory Systems 

In invertebrates sensitivity to near-UV is quite common. However, some rodents (mice, gerbils and gophers) also feature a peak in sensitivity at 359 - 511 nm. In these rodents and some marsupials sensitive to UV, it is thought to be attributable to a specific dedicated type of cone sensitive to near-UV (Jacobs et al., 1991; Winter et al, 2003). Near-UV is referred to as UV-A, encompassing 315 - 380 nm.  Likewise, birds seem to feature a dedicated, fourth cone class to detect UV (Benett &amp; Cuthill, 1994). 

In the color-blind flower bat, UV sensitivity has been attributed to a photoreceptor with two peak sensitivities - one in the green range and the other around 365 nm and running down to 310 nm (Winter et al, 2003). Hence, UV sensitivity can be conferred by cones with broad sensitivities ranging into the UV range.

In fact, the aphakic human eye (eyes with the lens removed after cataract surgery are aphakic) has been shown to be sensitive to near-UV. As can be seen in Fig.1, the blue cones are actually pretty sensitive to near-UV. It appears that the lens absorbs much of the UV, rendering UV light useless to humans with healthy eyes (Griswold &amp; Stark, 1992).   

Infrared (IR) sensitivity in snakes is mediated by pit organs and not via the eyes. Basically they are heat sensors and likely don't mediate vision as such (Newman et al., 1982). I was not able to find any relevant information on IR vision, or how it should be mediated. In fact, I doubt IR is used for vision at all, i.e., to reconstruct the visual scene. IR vision is used more in the form of general heat detection.

References
- Benett &amp; Cuthill, Vis Res (1994); 34(11): 1471–8
- Griswold &amp; Stark, Vis Res (1992); 32(9):1739-43
- Jacobs et al., Nature (1991); 353: 655-6
- Newman et al., Sci Am (1982); 246(3): 116-27
- Winter et al, Nature (2003); 425: 612-4