Thank you for acknowledging that this problem exists. As a student who "suffered" from working in groups I think I have the duty to give my two cents to this question and point out that some suggestions from others are bad. I'm a student and I've been there countless times. Oh, I ended up hating working in groups.
TL;DR: Putting students to work in groups has a mountain of downsides. First of all, be aware of them. And at least, you MUST have a solid way of grading each student separately.
The fallacy of "but in real world they will work in groups"
Well, it's true that in the real world, almost everyone will have to work in a group, and it is lovely to think that students must be trained beforehand so they can do that properly. But working in a group in a real job is one thing. Lots of "group assignments" I see around in schools / universities are totally another thing.
In real life you will have a manager/boss that will "grade" and "fire" you based on what you do. On the other hand, in the vast majority of group assignments I've received as a student, everyone in the group get the same grade, no question about it. This is borderline ridiculous.
In real life you will have a manager/boss that will give you tasks, observe your progress, help integrate your work with the work of others, make sure things are going smoothly, and so on (or, if he doesn't, then he is a bad manager, but nevertheless in real life it is not your job to manage the stuff - unless you're the manager of course, but this is not in the scope of this question). In undergrad group assignments, good luck to you, just get the whole thing done and hand in to the teacher.
In real life each member of the group can be assumed to be minimally interested in doing a good job (otherwise they will be fired). In undergrad group assignments, there is always that guy who is just slacking. And for the "suggestions" of letting students "fire" slackers or grade each other on how much they helped: that's nonsense. First of all students aren't supposed to grade each other, period. Secondly, put some psychology in it. No student will ever give terrible grades or "fire" another student. There's a whole lot of social aspects here. Bullying, retaliation. They belong to the same class, they have other classes to attend, perhaps will be colleagues for years, whether they like it or not. Do you think I would expose the bully slacker or do his work for him and stay safe?
Unless the theme of your class is 'working in groups', where you would assign someone to be the manager and all that, it is probably not your place to grade these oddities of working in groups. And by not grading it, it brings the morale of the good students down, since they have a ton of unrewarded extra work and stress.
Ask yourself why you are putting students to work in groups.
Perhaps you are assigning a huge task but don't want it to be done individually because it's too much work for a single person. You are counting on the fact that if they are working in a group they will split the work and then have less work per person. If this is the case, don't forget and don't underestimate the fact that if they split the work, there is the extra task of adding it up in the end. Oh, this task is the worst, brings me memories... First of all, don't underestimate the hugeness of this task. How many times it happened to me that I said "okay guys send me your parts and I will add them up" only to find out that they aren't joinable at all, sometimes contradictory, sometimes missing a key thing to glue them... Oh, but it's a part of working in groups, you might say. It is lovely that you want your students to learn the extra difficulties of working in groups. But we get again to what I said in the section above.
Perhaps you want them to learn to work in groups. If this is the case, make sure you understand what I said in the other section. If you are aware of all the problems, and still think you can find a fair way to do it, I'm sure you will do way better than many other teachers out there that just throw the work in the students without thinking about it. But is it really your task to teach students to work in groups? I mean, is it in the spec of your teaching subject?
Solution: (other than giving up on group assignments) split the tasks yourself!
Since this is about programming, this shouldn't be so hard actually! Example:
Your task is to create a tic-tac-toe variation game, with a 7x7 board, where connecting six symbols is a win. Organize yourselves in groups of three. One person will code the game mechanics (the concept of a board with methods to make a move and determine the winner), another will code the user interface, and the third will code a ranking system for accounting the best players among many matches.
(I just made that up, certainly needs tweaking) The idea is that each member is doing something specific. And if one of them does their part badly, the others don't have to worry about it. Everyone will be mainly graded by their module. Considering Java, for example, you could specify the interfaces and tell them to implement them. Or you could make that a task too, each member explains how he expects the other member's APIs will be. This way they can sit down and discuss, if they are in the mood, or just imagine what the APIs of the others should be and base their work on that - and of course explaining why the APIs should be like that could be graded as well);
If you can't find an easy way to split the tasks yourself, imagine how harder it would be for the students! In this case, reconsider again why you're giving a group assignment.