The Organic groups module (as already suggested in another answer) is indeed a possible answer to your question. But you should also have a look at the (fairly new) alternative for it, which is the Group module (with it roots close to where Dries started Drupal ...). Some more details about it (from its project page):
Organic Groups allows content itself to be groups, which isn't always what people want. It relies on an entity reference field to keep track of the ties between a group (node, term, ...) and its content (node, term, user, ...).
Groups instead creates groups as entities, making them fully fieldable, extensible and exportable. Every group can have users, roles and permissions attached to it. Groups can also act as a parent of any type of entity. Seeing as a group itself is also an entity, creating subgroups is very easy.
Even though it only has a beta release for D7 so far, its Usage statistics seem to indicate it is like a "rising star". And I've heard it mentioned recently in various occasions as a valid alternative for the "heavy" Organic groups module.
The Group module also integrate nicely with the Rules module, as explained in comment #2 of issue 2603136, which states the following:
... you can already use Rules to:
- Add a new Group
- Create a new GroupMembership and save it (the equivalent of $group->addMember())
- Add or remove a GroupRole from a GroupType
- React on new GroupMembership or Group entities
What hasn't been done yet is custom Rules actions or conditions. Seeing as 90%+ of Group is pure Entity API CRUD-operations, there has been no custom Rules code yet given how much you can already accomplish out-of-the-box.
Useful Rules we may add could be:
- E-mail all members of a Group, optionally filtered by GroupRole
- Easier-to-understand labels for the above list: "Member joined group" sounds easier than "GroupMembership entity is created"
Refer to Integrations with other modules and its "Related issues" about other modules for which integrations already exist, or are in the pipeline.
PS: Refer to How to implement an option for users to limit access to nodes/comments to users with the same role while posting it? for an interesting illustration of how this module can be used.