Is there an easy way - from the administrators view - to find out all user groups attached to a certain user? Do I have to fetch these data from secondary_group_ids in xf_user or from the table xf_user_group_relations?
And when the administrator wants to attach a new user group to a user, which of those tables has to be updated and how?
Is it also possible to get all users attached to a certain user group?
Look in the library/xenforo/model/user.php file. You want to avoid adding and removing users from groups with a simple query because of the way users are cached. You can also see some useful functions for getting users based on their group.