$! gives you the pid of the last run process. You often see it used to log the process id to a file like
echo $! > programname.pid
If you are piping one command to another then $! will give you the process id of the second command since it is the last. As in
command1 | command2 &
How do I get the process id of the first command?