In some sad cases, children learn much more than they should about an affair. Great care should be taken to give them appropriate information at the appropriate time.
If they don't already know, what should you tell them?
You need to be honest with your children, but what you tell them depends on their ages and how much detail they already know.
Ages 13 or more, at home: It's likely that they already know or, at least, have guessed what is happening. If so, don't lie about it. Without getting into details, you should admit what happened, but then assure them that it is over and that you are taking steps to fix your marriage. (Caution: Don't tell them this if it isn't true. Telling lies just to assure and comfort them may buy some temporary peace, but it ultimately do more damage to their trust in you.) If they have questions, answer them. Keep the lines of communication open with your children and don't be afraid to check in with them periodically to provide assurance and to answer any new questions they might have.
Ages 8-12: Provide fewer details, but if they have any awareness of the affair, you need to admit that you got too close to someone else for a while, but that it's over now. Take responsibility. Give them plenty of reassurance with your words and your touch. Let them ask questions and provide answers that are general but honest.
Under age 8: Your child is likely too young to understand anything about an affair, but they'll certainly have some sense of the conflict in your marriage. Address the conflict. Let them know the two of you are having some problems that you are working on. Assure them that they have no responsibility for the conflict. Children that age tend to view the world as though they are at the center of it and will assume that your problems are their fault.