I think that the main reason is legibility.
Monospace are designed to make characters easy to read and find in hundreds of lines of code.
In the other hand, serifs and sans serifs fonts are designed to be used to improve legibility in large amount of texts, example books, magazines, and so on.
This text may help you: Proportional Vs. Monospace Fonts
Benefits and Disadvantages of Monospace Fonts
Setting text in a monospace font makes it easier to identify characters by themselves. Because of this, tasks that rely on the easy identification of specific characters, such as programming, benefit from the use of a monospace font. Similarly, a monospace font may be used to format code examples within a page that is otherwise set in a proportional font, in order to make them stand out more easily. Text set in a monospace font is also easier to align, leading to the creation of images constructed using characters, known as "ASCII art."
On the other hand, because of the fixed width of all the characters, a
block of text set in a monospace font will typically take up more
space than the same text set in a proportional font. Additionally,
long stretches of monospaced text can blend together visually and, as
a result, become harder to read.