The tar command has an option to do that in one shot:
Code:
tar -zcf archive.tar.gz /path/to/dir
-z is intended to compress the archive using gzip. Other options let you select other compression algorithms, for example -j for bzip2, --lzma for lzma and so on. You have also the -I option to select any compression program available on your system arbitrarily.
Anyway, a working version of your attempt is:
Code:
tar -c /path/to/dir | gzip > archive.tar.gz
without
-f <archive name> tar sends the output to the standard output. gzip grabs it through the pipe and redirects to the compressed archive.