The code can either be added to your theme’s functions.php file, or to a separate plugin. It would be best to use a development site to test the code before adding it to your main site.
The condition will need to return true if you don’t want the user to be able to login to Discourse, so you’ll probably want to check that the user does not have the required membership level. You’ll need to look at the s2Member documentation to find out how to do this. I’ve never used the plugin, but it looks like you can get the membership level for the current user with:
get_user_field( 's2member_access_level' );
If that is correct, the condition would be something like this. (You’ll need to test this):
if ( ! 1 >= get_user_field( 's2member_access_level' ) ) {
}
Yes, you can redirect the users to any page you like.