It's probably coil whine.
Can definitly get annoying, but it doesn't affect your pc in any bad ways.
It can also come from your graphics card.
Motherboards can also coil whine, but it's definitly more rare to have complains on that side.
The noise changes, is due to the load changing on the CPU and/or GPU. So when your game is in the background, obviously it tend to consumes less resources on your pc (the game achieve that oftenly via a relativly low frame cap when you're not focusing on it), and when you're re-focusing on the game : Boom, full fps.
As your fps increase, it will consumes more resources and so more power, which make thoses coils whine again.
The intensity of a scene in game can also make the sound change, which is also a bit annoying. But again, it doesn't affect anything except yourself.