Writing CSV files is very easy. "CSV" means "comma separated values". A CSV file consist of records; each line is a record, and the fields in each record are separated by commas.
You just write a program that opens the file for writing, first write the header line, then loop through the records, for each record write a line with the data for that record (fields separated by spaces), then close the file.
CSV does not have a concept of headers. It has rows and columns of values. ANy program processing a CSV file is of course free to treat certain rows or cells in special ways (as if they were headers).
It gets a little more complicated that Jesper described, though. Sometimes the delimiter is not a comma but a semicolon, and if the delimiter occurs inside of a cell, then the cell contents need to be enclosed in double quotes. And if the contents contain double quotes or newline characters, then those need to be escaped.
Before you've coded up all these special cases, you might as well use a ready-made library for reading and writing CSVs. The AccessingFileFormats FAQ page lists several such libraries.