Your operating system provides so called system calls to dynamically allocate memory (malloc, free, etc.), it also provides the mechanism to load and execute your program.
When the program is loaded by the os the text segment (code memory) is set up and the statically allocated memory in your program is immediately available. As your code calls functions, the (statically allocated) variables in your functions are allocated on the stack and your dynamically allocated memory (using malloc() for example) is allocated on the heap. During the time your program runs, it is your (the programmer's) responsibility to manage the memory (lack of doing so will lead to memory leaks and will eventually cause a long running program to run out of memory and it will crash, or in extreme cases, depending on the OS, take the whole OS down with it).
See also this article: http://www.maxi-pedia.com/what+is+heap+and+stack