How are neurons selective towards specific stimuli?
Sensory receptors and neurons get their selectivity from physical processes and their position in space. For example, photoreceptor cells in the retina respond to light in a specific position in space because the lens and structure of the eye directs light incoming in a particular angle to a specific spot on the retina. Sensory receptors in the skin can sense touch in a particular place because that's where they are located. Hair cells in the cochlea respond to particular frequencies of sound because the membranes in the inner ear vibrate at different frequencies along the length.
For selectivity to lines of a particular orientation, neurons combine receptive fields of retinal cells, like this:

From https://grey.colorado.edu/CompCogNeuro/index.php/CCNBook/Perception
The &quot;LGN&quot; (visual thalamus) cells pictured here have receptive fields that look a lot like the retinal ganglion cells that carry output from the retina. For simplicity, you can think like they are the same. These cells are excited by light in the center (red), and inhibited (indirectly) by their neighbors (blue) (there are also &quot;off-center&quot; cells that respond in the opposite way, preferring dark in the center and light in the surround).
If you add up a bunch in a row, you can make a cell in primary visual cortex (V1, the green cell) that responds to edges. If you summed up the receptive fields of cells in a different orientation you'd get selectivity to a different orientation of lines. The key feature to making selective receptive fields is which cells are connected to which.
These receptive fields are created in development by spontaneous activity in the retina and eventually by actual visual input when the eyes open. The process is pretty complex and occurs in multiple stages, but you can start with a review like this one:
Huberman, A. D., Feller, M. B., &amp; Chapman, B. (2008). Mechanisms underlying development of visual maps and receptive fields. Annu. Rev. Neurosci., 31, 479-509.
Higher in the visual processing hierarchy you will find increasing complexity as receptive fields of different types are combined and recombined.