How can I make a shortcode from this code?

It should be as simple as this:

function item_count() {
    $dir="/PATH TO DIRECTORY/";
    $filecount = 0;
    $d = dir( $dir );
    while ( $f = $d->read() ) {
        if ( ( $f!= "." ) && ( $f!= ".." ) ) {
            if( ! is_dir( $f ) ) {
                $filecount++;
            }
        }
    }
    return '(' . $filecount . ')';
}

add_shortcode( 'count', 'item_count' );

If that’s still giving you a Headers Already Sent error, then check to make sure that there’s no whitespace at the end of your PHP file. (You can safely omit the closing ?> tag, which will help you ensure that there’s not any extraneous whitespace.)

See this answer for more on omitting the closing PHP tag.