When creating a post how do you select the format?

As post_format is a custom taxonomy and the different formats terms, you should be able to use the tax_input parameter in the new post args array to set the format. Something along these lines,

$new_post_args = array();

// e.g. $format_slug = 'gallery';
$valid_formats = get_post_format_slugs();
if ( 'standard' !== $format_slug && in_array( $format_slug, $valid_formats ) ) {
  $new_post_args['tax_input']['post_format'] = 'post-format-' . $format_slug;
}

wp_insert_post( $new_post_args );