How to make number of blog posts a custom field?

It might actually still work, but the syntax is currently wrong (you have a PHP opening tag inside another opening tag). And you can assign the value of the custom field to a variable using the get_field function. What if you try this?

<?php
// assuming your ACF name is 'number_of_posts'
$numposts = get_field('number_of_posts');
$args = array( 'numberposts' => $numposts );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post);
?>