Hi, Ryo!
The others have explained the meaning very extensively already, but I wanted to add that "What the devil?" Is hardly used nowadays.
I instantly think of Sherlock Holmes, for example, when I hear or read this. It is also used mockingly, as you might also hear someone from a lot of money using this expression. I am speaking purely from a British standpoint, mind you, so I can't speak for the rest of the world in terms of frequency.