If we type a command in a shell it basically creates a child by calling fork and then the child exec's the command we type in, so the parent of it is the shell. If it is a foreground process the parent(in this case shell) has to wait for child to terminate and if we type & it executes in the background. So what exactly makes the parent to wait or not to wait for the child to terminate?
As Eric Renouf commented, if you want to wait for a child process in your own code, you should use the
wait function (typically implemented as both a system call and a C library function, so you'll find manpages in both section 2 and 3). The linked page has an example implementation.
If you want to do the same thing in shell code, you can use the
wait command which waits for a backgrounded job to finish.