I read in some places that music is mostly sampled at 44.1 kHz whereas we can only hear up to 20 kHz. Why is it?
- The sampling rate of a real signal needs to be greater than twice the signal bandwidth. Audio practically starts at 0 Hz, so the highest frequency present in audio recorded at 44.1 kHz is 22.05 kHz (22.05 kHz bandwidth).
- Perfect brickwall filters are mathematically impossible, so we can't just perfectly cut off frequencies above 20 kHz. The extra 2 kHz is for the roll-off of the filters; it's "wiggle room" in which the audio can alias due to imperfect filters, but we can't hear it.
- The specific value of 44.1 kHz was compatible with both PAL and NTSC video frame rates used at the time.
Note that the rationale is published in many places: Wikipedia: Why 44.1 kHz?
44,100 was chosen by Sony because it is the product of the squares of the first four prime numbers. This makes it divisible by many other whole numbers, which is a useful property in digital sampling.
44100 = 2^2 * 3^2 * 5^2 * 7^2
As you've noticed, 44100 is also just above the limit of human hearing doubled. The just above part gives the filters some leeway, therefore making them less expensive (less chips rejected).
As Russell points out in the comments, the divisible by many other whole numbers aspect had an immediate benefit at the time the sample rate was chosen. Early digital audio was recorded on existing analog video recording media which supported, depending on region, either the NTSC or PAL video spec. NTSC and PAL had different Lines per Field and Fields per Second rates, the LCM of which (together with the Samples per Line) is 44100.
The Nyquist rate is above twice the bandlimit of a baseband signal that you want to capture without ambiguity (e.g. aliasing).
Sample at a lower rate than twice 20kHz, and you won't be able to tell the difference between very high and very low frequencies just from looking at the samples, due to aliasing.
Added: Note that any finite length signal has infinite support in the frequency domain, thus is not strictly bandlimited. This is yet another reason why sampling any non-infinite audio source a bit above twice the highest frequency spectra (in a baseband signal) is required to avoid significant aliasing (beyond just reasons of finite filter transition roll-off).
Basically, twice the bandwidth is a common requirement for signal sampling, thus $2\times 20 = 40$ kHz is a minimum. Then, a little more is useful to cope with imperfect filtering and quantization. Details follow.
What you need in theory is not what is required in practice. This goes along the quote (attributed to many):
In theory there is no difference between theory and practice. In practice there is.
I am not an expert in audio, but I have been trained by high-quality audio sampling/compression people. My knowledge might be rusty, take it with caution.
First, standard sampling theory works under some assumptions: linear systems, and time invariance. Then, a continuous bandlimited phenomenon is known, in theory, to be possibly sampled at about twice the bandwidth (or twice the maximum frequency for baseband signals) without loss. The "Nyquist rate" is often defined as:
the minimum rate at which a signal can be sampled without introducing errors
This is the analysis part of the "sampling theorem". The "can be" is important. There is a synthesis part: the continuous signal "can be reconstructed" analogously using cardinal sines. This is not the only technique, and it does not take into account low-pass prefiltering, non-linear (such as quantization, saturation) and other time-variant factors.
Human hearing is not a simple topic. It is accepted that humans hear frequencies from 20 Hz up to 20,000 Hz. But such precise bounds in Hertz are not a trait of nature for all humans. A gradual loss of sensitivity to higher frequencies is frequent with age. On the other side:
Under ideal laboratory conditions, humans can hear sound as low as 12 Hz and as high as 28 kHz, though the threshold increases sharply at 15 kHz in adults
Hearing is not linear: there are audition and suffering thresholds. It is not time-invariant. There are masking effects in both time and frequency.
If the 20 Hz up to 20,000 Hz band is a common range, and a 40,000 Hz should theoretically suffice, a little extra is needed to cope with extra distortion. A rule of thumb says that 10% more is ok ($2.2\times$ signal bandwidth) and 44,100 Hz just does it. It goes back to the late 1970s. Why is not 44,000 Hz used? Mainly because of standards, set by the popularity of CDs, whose technology is as always based on a trade-off. In addition, 44,100 is the product of squares of first four prime numbers ($2^2 \times 3^2 \times 5^2 \times 7^2$), hence has small factors, beneficial for computations (like FFT).
So from $2\times 20 $ to $44.1$ (and multiples), we have a balance in safety, quantization, usability, computations and standards.
Other options exist: the DAT format for instance was released with 48 kHz sampling, with initially difficult conversion. 96 kHz is discussed with respect to quantization (or bit depth) in What sample rate and bit depth should I use? This is a controversial subject, see 24 bit 48kHz verses 24 bit 96kHz. You can check Audacity sample rates for instance.
Why it is exactly 44.1 kHz has been already answered - but to focus on the aspect of your question related to the limit of human perception, the reason is quite simple.
The resolution in time has to be fine enough to be able to generate all possible wave forms up to the limit that is perceptible. According to the sampling theorem, the resolution has to be such that the sampling frequency is at least twice this frequency. Intuitively, at the highest frequency, you need at least 2 points to represent the maximum and the minimum of your signal - giving this Ascii-art square wave:
_ _ |_| |_
In order to faithfully reproduce a signal, the faster the sample rate the better. ~40 kHz was chosen, because it was a low sample rate that most people can't tell the difference for (when reconstructed). When audio sampling was introduced, memory and storage was expensive and higher sample rates were not cheaply possible.
At double the upper limit of human hearing two samples per cycle is very poor reconstruction, even if it meets the Nyquist criteria for sampling signals, a simple chart depicting a sine wave with two samples per cycle will show you how poor two samples per cycle is in reproducing a waveform. You can literally turn a sine wave into square wave; it is a good thing at 20 kHz nobody can tell. I bet a dog could though.