How do I insert a post with custom post type and relate it to a custom taxonomy?

Try changing this line

  wp_set_object_terms( $post_id, array($cat_ids), 'categorycourses', false);

to

  wp_set_object_terms( $post_id, $cat_ids, 'categorycourses', false);

$cat_ids is already an array and you are writing with in array again.
Hope it helps.