How do I redirect users to the page they were before logging in to my site?
Have a look at the Login Destination module.

In custom code, to issue a redirect in Drupal use the drupal_goto() function. Drupal has a massive API, use it.