If the group ID bit for the directory is off, the group used is a random selection (probably the last loginable group of the user) from the groups to which the user belongs.
If the group ID bit is set, the group for the file is taken from the directory in which the file is being created. In this case, it does not matter if the user is a member of that group.
To set the group ID bit on a directory,
chmod g+s directoryTo clear the group ID bit on a directory,
chmod g-s directory