Does the iPhone close the background programs if it runs out of memory or battery?
There are two ways to approach this question.
From the end user's perspective, the answer is no. No matter what you do, the app will come back to the same state it was in previously, unless you close it from the switching interface.
Technically: yes. When the device runs short on RAM, it will freeze the application's state from RAM to the main storage (flash). When you resume, it loads the state from flash back into RAM, and then resumes. This is intended to happen quickly enough and transparently enough to be indistinguishable, but you may sometimes notice that resuming takes a bit longer if you have loaded several other apps in the meantime (and therefore pushed the app out of RAM).
They aren't really "in memory," more like cached to disk if and when necessary. Many apps don't even use the multitasking or aren't setup for it. When you switch it does actually close the app.
Being in the task bar doesn't guarantee that it's actually in memory, actively running, or both. The OS manages that.
No. Even restarting, complete power down and power up, will not remove the background applications from memory. According to the Apple Geniuses you must manually remove the applications from the task bar.