Why is everything in Linux treated as a file?

Because it unifies the input and output (I/O) under the same interface.

Devices connected to the computer are also shown as files in the file system under the /dev directory