How could O'Brien write the book as Goldstein while still remaining loyal to the party and never seeing any flaws in the system?
There are several motivators for O'Brien to write the book, and those motivators actually further prove O'Brien's fidelity to the Party.
1. The book serves as an entrapment device for traitors to the Party. When O'Brien (and the other implied authors) writes the book, he knows that it will help him weed out the "weak" who are not in full agreement with the Party's goals. Thus, his authorship is of great importance to the Party's continuing existence because it allows the Thought Police to round up would-be traitors.
2. O'Brien willingly admits to Winston that the Party has no noble goals and wants power simply for power's sake. So, the book literally discusses the true goals of the Party, and O'Brien does not have a problem with that because he does not try to hide the Party's goals from Winston. He knows that by the time he's done with Winston and other dissenters that they will be brainwashed to believe whatever he wants them to believe and die soon afterwards.
3. The word "flaws" implies moral or ethical problems with the Party and its goals, but O'Brien and the Inner Party could care less about the immorality of what they are doing or striving for. The only type of flaw that would concern O'Brien is one that would hinder the Party's self-serving goals, and O'Brien is confident that that type of flaw has been eliminated.
I think there are a couple of ways this could happen.
First, I know that I could write critiques of things I believe in that would make them sound bad. I could know what other people might think was bad about my system and I could write a critique based on that. I wouldn't necessarily believe what I wrote, but I would be able to write it.
Second, remember that O'Brien is part of an Inner Party that can come up with "black is white" and "2+2=5." As such, he must have a pretty great capacity for tricking himself. So he may be speaking the truth (as he sees it) when he says that the book accurately describes the society but its programme is rubbish.
I hope that makes some sense...
There are several possible answers.
1. O'Brien wrote the book using and used doublethink to immediately forget it. I think this pretty unlikely.
2. O'Brein realizes the book he writes is true and sticks to the lies of the party to gain more power. I think this moreso unlikely.
3. O'Brein is a lunatic and believes in the party's view of reality but understands the perception thought criminals have and puts those views to words. This is probably the most likely.
4. There is a "Brotherhood" and O'Brein and the party capture some of the copies of "the book" and send it to thought criminals themselves. In this way, the thought criminals would embody the spirit of the Brotherhood and the party breaking their spirit and having them love Big Brother would show their "curing" system is undefeatable. This just might be crazy enough to be true.