How to programatically toggle the media setting “Organize my uploads into month- and year-based folders”?

The (database) option name for that setting is uploads_use_yearmonth_folders, so you can use update_option() to programmatically change the setting’s value:

// 1 to enable the "checked" state
update_option( 'uploads_use_yearmonth_folders', 1 );

You can also use a filter hook such as option_<name>:

add_filter( 'option_uploads_use_yearmonth_folders', function ( $value ) {
    return 1; // 1 = checked
} );

Or am I not understanding the question? 🙂