An overflow of suds is what you get when you use too much laundry detergent, or when you use a cleaning compound that is not intended for use in a washing machine.
If you are using an appropriate product and you are getting excess foam (foaming out of the machine, or residual foam in the garments, or foaming out of the drain), cut the amount you use in half. If you continue to get foam, cut it in half again, and so on. For example, if you normally use 1 cup, reduce to ½ up, then to ¼ cup, and so on. Do this until you eliminate the foam problem.
If at that point you are not getting adequate cleaning, increase the amount by ½. For example, if you reduced the amount to ½ cup, try ¾ cup. If you cannot get adequate cleaning without foaming, your detergent is at fault. Get a better quality detergent and start over.
If you have a top loading machine, you can use any standard machine laundry detergent. If you have a front loading machine, you should limit yourself to detergents which are compatible with front loaders. These are often labeled "high efficiency" detergents. Foaming products, such as those made for manual washing of delicate garments, and other foaming products such as hand dish soap or soaps for hand cleaning, should never be used in a washing machine.
This answer is based on information I learned while managing a detergent manufacturing company and laboratory.