How is a program stored before compiling?
Here is a brief overview of a typical compilation and execution flow on a Von Neumann machine:

Type in the source code in a plain text editor.
This source code consists of characters. Characters are encoded as bits. An example of encoding is ASCII. These bits are stored on the disk, when you save the source code.
Invoke the compiler to compile the source code. The compiler parses the source code file(s), produces object code and links the object files to produce a machine language file called executable. The executable is stored on the disk.
Run the executable. This is called execution.


At all steps in the process, the operating system controls and co-ordinates processes and does the memory management.

Data is stored in the RAM temporarily when the process running on the machine requires it.

Data is stored on the disk when it needs to be stored permanently.

There is also a process called swapping which is done by the OS when there is no RAM memory left. A section of the disk memory is used for swapping.

There can be multiple processes running on the machine. These processes can run parallely or concurrently. The operating system allocates resources for processes and schedules the processes to run using scheduling algorithms.


This was just a brief overview. To learn more, I highly recommend the free online course on MIT's OCW called Computational Structures.