If you compile a program on a particular OS, that it is that OS that you should run it on. For example, if you compile a command line program for Windows, it will work on Windows, but it will not work on Ubuntu. You said yourself that the content of the executable file differs from OS to OS.
What you can do is to use an emulator for a particular system on another system. For example, if you are on Linux, you can use Wine to run Windows programs. In this way you emulate the Windows environment for your program even though it is on Linux.
Of course, the CPU should follow the instructions as they are. If you, e.g., want to print a hello world line, than your program contains code for that. But not just for that. It contains the code for other stuff that are OS dependent, and this is where the problem is. For example, your Windows compiled program might use some Windows API to perform the printing, and this API is not to be found on Linux. It is than the called API, and not your program that directly performs the output.
As Jongware mentioned it in the comment below, what you could do is to cross-compile your code. In that case you can compile your code on Windows so that it can run on Linux, but only if you compile it with the libraries needed for the specific Linux. In that case, however, you will not be able to run you code on Windows.