While in the Jakku system, Rey had to repair the Falcon, and asked Finn where the Resistance base was. Not knowing the answer, he admitted to BB-8 (but not to Rey) that he wasn't part of the Resistance and asked where the base was. After some debate, BB-8 revealed the base's location in the Ileenium system.
So why do they go to the Maz's castle, rather than the Resistance base?