How to get group ids by ids of elements of group content?
The way Group module maintainer recommended:

use Drupal\group\Entity\GroupContent;

function getGroupIdsByEntity($entity) {
  $group_ids = [];

  $group_contents = GroupContent::loadByEntity($entity);
  foreach ($group_contents as $group_content) {
    $group_ids[] = $group_content-&gt;getGroup()-&gt;id();
  }

  return $group_ids;
}