:w save (write to disk) :q exit
Make sure you are in command mode by pressing the [Esc] key. Now type :w. This will save your work by writing it to a disk file.
The command for quitting vi is [q]. If you wish to combine saving and quitting, just type :wq. There is also a convenient abbreviation for :wq --- ZZ. Since much of your programming work will consist of running a program, encountering a problem, calling up the program in the editor to make a small change, and then exiting from the editor to run the program again, ZZ will be a command you use often. (Actually, ZZ is not an exact synonym for :wq --- if you have not made any changes to the file you are editing since the last save, ZZ will just exit from the editor whereas :wq will (redundantly) save before exiting.)
If you have hopelessly messed things up and just want to start all over again, you can type :q! (remember to press the [Esc] key first). If you omit the !, vi will not allow you to quit without saving.