Query add html after set amount of posts?

Here’s one way of doing it:

<?php
$courses = get_posts( array(
    'post_type' => 'courses',
    'posts_per_page' => -1
    ) );

if ( $courses ) {
    print "\n" . '<div style="background:pink">';
    foreach ( $courses as $course_count => $post ) {
        setup_postdata( $post );
        the_title( "\n", '<br>' );
        if ( 5 == $course_count ) {
            print "\n" . '</div>';
            print "\n" . '<div style="background:yellow;">';
        }
    }
    print "\n" . '</div>';
}

wp_reset_postdata();
?>