If you want to redirect users who land on a non-existent page (404) to the homepage, add the following code. Note: Google recommends showing a custom 404 page. For more on what causes 404 errors and how to handle them.
add_action( 'template_redirect', function() {
if ( is_404() ) {
wp_redirect( home_url(), 302 );
exit;
}
} );