$! gives you the pid of the last run process. You often see it used to log the process id to a file like
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
How do I get the process id of the first command?
echo $! > programname.pid
command1 | command2 &