A lot of it comes from years of use, though if there are a lot of type match-ups that you're trying to master, you could try an electronic flashcard program. I haven't tried using one for Pokemon types, but you could easily go over them that way while procrastinating, and it'll be easier to focus on each pair than it is when studying the giant 18x18 grid.
For a more hands-on approach, try playing a challenge run with a Pokemon whose type match-ups you're struggling with (e.g., do a solo run). By the end, after seeing all of those "super effective" and "not very effective" messages, you'll probably have its type match-ups down.
As for me, if I'm struggling with a type match-up, I try to rationalize it to myself somehow, even if it doesn't seem immediately obvious. Or, for a couple of them (mainly dark and fairy), I think about why they were added to remember some of the types (e.g., psychic was overpowered in gen 1, so Game Freak introduced dark to balance it out). I'll admit, though, I'm still struggling with fairy's match-ups (aside from dragon and poison), and I'm having more trouble committing those to memory than I ever did with the rest of the types.