Limit categories and it’s subcategories for specific group of users

Problem with your code is that you don’t modify queries at all in any way, so all queries on your site still get these posts – so they are visible on lists. And later, you use template_include filter which allows you to redirect single post view and single category archive if it shouldn’t be visible for given user… Also redirecting to home page in such case isn’t best – it may confuse users…

So how to hide these posts properly, so users won’t see them at all?

You should use pre_get_posts filter. This way you can check if current user can see these posts and hide them, if he shouldn’t.

function check_user() {
    if ( ! get_current_user_id() ) {  // this way you won't get notices when user is not logged in
        return false;
    }

    $user = wp_get_current_user();
    $restricted_groups = array('company1', 'company2', 'company3', 'subscriber'); // categories subscribers cannot see
    if ( array_intersect( $restricted_groups, $user->roles ) ) {
        // user is a subscriber or restricted user
        return false;
    }
    return true;
}

function restrict_users_categories( $query ) {
    if ( ! is_admin() ) {
        if ( ! check_user() ) {
            // change 1, 2, 3 to IDs of your excluded categories
            $cats_excluded = array_merge( array(1, 2, 3), (array)$query->get( 'category__not_in' ) );  // don't ignore any other excluded categories
            $query->set( 'category__not_in', $cats_excluded );
        }
    }
}
add_action( 'pre_get_posts', 'restrict_users_categories' );

Also… There is something a little bit odd in your check_user function… If you want to check if user has one of these roles ‘company1’, ‘company2’, and so on, then it’s OK.

But if you want to restrict only subscribers, then it should be:

function check_user() {
    if ( ! get_current_user_id() ) {  // this way you won't get notices when user is not logged in
        return false;
    }

    $user = wp_get_current_user();
    if ( in_array( 'subscriber', $user->roles ) ) {
        // user is a subscriber
        return false;
    }
    return true;
}