You can not use wp_redirect()
if the headers have already been sent so snippet you provided should not be used within your template files, instead you should wrap it within a function and place it into your functions.php file and hook onto template_redirect
action.
Example:
add_action('template_redirect', 'front_end_post', 99);
function front_end_post() {
if( 'POST' == $_SERVER['REQUEST_METHOD']
&& !empty( $_POST['action'] )
&& $_POST['action'] == "new_post")
{
// your logic here
//your redirect
$url = get_permalink( $pid );
wp_redirect($url);
exit();
}
}