We describe in this paper a connection between bifix codes, symbolic dynamical systems and free groups. This is in the spirit of the connection established previously for the symbolic systems corresponding to Sturmian words. We introduce a class of sets of factors of an infinite word with linear factor complexity containing Sturmian sets and regular interval exchange sets, namemly the class of tree sets. We prove as a main result that for a uniformly recurrent tree set $F$, a finite bifix code $X$ on the alphabet $A$ is $F$-maximal of $F$-degree $d$ if and only if it is the basis of a subgroup of index $d$ of the free group on $A$.