Groups can be randomly assigned, self-selected or assigned by the tutor.
If students are forming their own groups, think about why this is consistent with the intent of your assessment task. Note that if students are early in their course of study, they may be more comfortable sharing in a self-selected group, when they feel that their group will not be making judgments about their lack of knowledge.
Alternatively, you may want to purposefully assign students to groups to take advantage of particular attributes of working in a group, e.g. give students opportunities to exhibit leadership and take on different roles, or take advantage of the diversity of knowledge backgrounds that the students have. You may also want students to work on giving and receiving feedback - this can be easier when students are less familiar with each other. Working in purposefully contrived groups emulates how students are likely to operate in a workplace.
Specify the size of groups, taking into account the nature of the task. Small groups find it easier to coordinate their efforts, but may struggle with excessive workload and lack of varied viewpoints. Larger groups can theoretically accomplish larger and more complex projects. There are no firm rules; somewhere between 3 and 5 is considered best.