To read data from a configuration object use \Drupal::config()->get('system.site')->get('name')
.
To set data to a configuration object you first need to load the editable configuration entity, then set the data and finally save it.
$site_settings = \Drupal::configFactory()->getEditable('system.site');
$site_settings->set('name', 'Foo site');
$site_settings->save();
To save custom configuration to the database, create a configuration yaml file:
modules/MODULE_NAME/config/install/custom.configuration.yml
custom-variable: 'hello world'
You should also probide a schema file for your configurations:
modules/MODULE_NAME/config/schema/custom.configuration.schema.yml
custom-variable:
type: 'string'
label: 'Custom variable'
description: 'A custom variable to store information in the database'