How does WordPress know if a URL doesn’t exist?

You don’t need to add anything special to the top of 404.php. WordPress will know to use 404.php automatically when it tries to get a post or page and fails.

To create a custom 404 page for a theme, the simplest way is to:

  1. Copy the index.php file from the current theme to a file called 404.php.
  2. Edit your new 404.php to delete the code dealing with displaying posts/pages and comments (basically, remove ‘The Loop’ but leave the header, main div structure, sidebar, footer).
  3. Replace the part you deleted with the HTML/PHP you’d like to display in its place (404: Sorry, ____ cannot be found).

I highly recommend taking a look at the 404.php that comes with the TwentyTwelve, TwentyThirteen (etc.) themes to get a grasp of how WordPress 404 error pages work.

I’ve never personally had a problem with WordPress being unable to tell a URL doesn’t exist, but if you want to make absolutely sure your web server returns your custom 404.php when a page can’t be found, add the following line to .htaccess in the main folder where index.php is:

ErrorDocument 404 /index.php?error=404

Just change the URL accordingly if you have WordPress installed in a subdirectory.

I hope this helps a little. Good luck!