Adding users to organic groups in CiviCRM
As @Graham says in his comment, you may want to use the CiviCRM OG Sync module provided with CiviCRM. If your interest is more in coding this, check out how the module does it at https://github.com/civicrm/civicrm-drupal/blob/7.x-master/modules/civicrm_og_sync/civicrm_og_sync.module.