How does a brain distinguish stimuli?
The fundamental way in which this works is by geography: signals triggered by different stimuli travel by different pathways to different regions of the brain that are specialised for dealing with the sorts of information they contain.

For example, visual signals from the retina pass down the optic nerve and (after a few interesting twists and turns that, inter alia, send the left and right halves of each visual field to different brain hemispheres) wind up in the visual cortex, which contains neuronal networks adapted for recognising different structures and regularities in the image. Whereas sound information from the ears, after an even more tortuous sequence of processing steps, winds up in the auditory cortex.

Along the way, the signals will typically be split up across many neurons and reintegrated into new signals that pick out different kinds of information. So a particular stimulus will result in a complex spatio-temporal pattern of action potentials firing across many regions of the brain.

Conceptually, the further along this process you go, the more abstracted the signal probably becomes, so that it stands for a collection of events and ideas and thoughts rather than for excited photoreceptors or vibrating hair cells. But exactly how this parsing of the sensory content eventually turns into perception and conscious recognition is essentially unknown.