It comes from a LOT of practice and a LOT of work developing an intuitive feel for the fundamentals. For instance, as soon as I saw your second problem, I immediately applied DeMorgan's theorem in my had and knew what the equivalent basic gate was. But, even with a quarter century of experience, I wasn't able to quickly figure out the first circuit by just looking at it. I probably could have given more time than I was willing to spend on it, but when the answer didn't come quickly, I pulled out a pen and snagged a nearby envelope and walked through the truth table step by step.It does amaze me at this point you can just look at these circuits and figure out what is going on, that is a VERY cool thing to be able to do.
One thing you definitely do need to do is internalize the basic logic gates. You need to be able to draw the truth tables or determine the outputs for a given set of inputs for the 1 input gates (BUF and INV) and the symmetric two-input gates (AND, OR, XOR, NAND, NOR, XNOR). If you have to memorize them at first, then memorize them. But eventually you need to simply understand what each one means. If you understand INV, AND, OR, and XOR in terms of the fundamental everyday meanings, then you can figure out all the rest.