Word processing programs, like "TextEdit" in OS X (or "WordPad" in Windows), can show the text as it will be formatted when it is printed, arranged in a particular way on the page, using different fonts. This is often called "WYSIWYG" (pronounced "whizzy-wig"), which stands for "what you see is what you get." The documents produced by these programs including formatting instructions and other information besides what you actually typed.
However, when you are writing software, your source code usually needs to be in "plain text" format, without the various formatting commands inserted by programs like TextEdit. You need to make sure that you are saving your file in "plain text" format, which typically requires you to use the "Save as..." menu item.
Text editing programs designed for writing software, on the other hand, write out their files in plain text format by default. You can adjust TextEdit using the "Preferences" menu.
The first menu contains the "New Document" settings.

There are other useful options in the "Open and Save" menu.

Two text editing programs that are very popular with programmers are vi and Emacs. Both of these programs have been around for a long time; vi's chief claim to fame is that it is installed by default in almost every variant of Unix. Emacs has also been ported to lots of systems, so if you aren't a systems administrator, the argument for vi based on availability isn't as strong. Emacs also supports a variety of editing modes for different programming languages, so it can be a very helpful tool in programming.
The EmacsWiki contains a good description of Emacs commands for the beginner. There is also a very good Emacs tutorial that you can run in Emacs itself. First, enter "emacs" on the command line in the Terminal application. The window contents will be replaced with some introductory information about Emacs displayed in an Emacs buffer. Then follow these instructions to start the tutorial.
Emacs can also run a shell in one of its buffers. The edit/test cycle then becomes a matter of switching between your editing buffer and the buffer that is running a shell. You switch between buffers with the ctrl-x b command. If you are going back and forth between two buffers, the other buffer is the default for selection by ctrl-x b so you can just enter return. In some ways, this can be more useful than the ctrl-z method. You can, for example, use Emacs commands to cut and paste the output of a shell command and use that output in another buffer that you are editing.