How does Hilbert's axiomatization relate to set theory? I'm studying Hilbert's axiomatization of Euclidean geometry, and I'm trying to combine my current understanding into my knowledge on mathematical logic (not very much).

At the beginning of this axiomatic system, it states the undefined terms are 'point', 'line', 'plane', 'lie', 'between', and 'congruence'. Therefore it seems that all later definitions should be based on these primitive notions. However, in the definition of 'segment', it states that a segment $AB$ is a set of two points on the same line. (or another variation where the segment contains also all points between two endpoints.)

My question is: how does 'set' come into play here? There is no notion of 'set' defined in this system, how is it valid to use the notion of set (or notions about sets like equivalent class) here? More generally, how does set relate to a specific mathematical theory?

Being a beginner on foundation related topics, I would appreciate if you can explain different concepts in a slow pace. In my understanding, Hilbert's system is a purely syntactic system with some undefined terms. It might be helpful if you can show me how the definitions (like segment) can be produced by FOL language.
The resolution is that Hilbert's axioms are not first-order axioms. They refer to sets (or "systems") as well as to individual objects.

This is most visible in the axiom of continuity, which directly refers to sets. The purpose of this axiom is to prevent issues which affect Euclid's axioms. One such issue is that the rational plane $\mathbb{Q} \times \mathbb{Q}$ satisfies many of the axioms of geometry (including the natural FOL versions of Euclid's axioms). Some axiom is needed to prove, for example, that the line $y = x$ intersects the circle of radius $1$ centered at the origin. 

Hilbert's axioms are most naturally viewed in second-order logic. In that framework, "set" is a logical concept (like "equality" and "or") rather than a term of the theory being studied.  

At the time that Hilbert was working on geometry, the distinction between first-order and second-order logic was not yet understood. So it is not at all surprising that Hilbert would not make any distinction, and would not view "set" as an undefined term of geometry. From the 19th century point of view there was simply "logic", which had not yet been formalized. It took until the 1940s or 1950s for our contemporary understanding of logic to be completely developed. 

There are first-order axiom systems for geometry, such as Tarski's axioms. These are different from Hilbert's axioms in various ways, but in particular they do not try to include a "completeness" or "continuity" axiom analogous to Hilbert's axiom.
One of the primitive notions in Hilbert's approach is the relation of *betweenness" which is a relation among three variables $A,B,C$ written as $A*B*C$. This reflects our intuitive notion of point $B$ belonging to interval $[A,C]$. Thus Hilbert is not relying on naive Euclidean notions when he talks of segments. A book that details Hilbert's approach and is more accessible than Hilbert's is Greenberg's Euclidean and non-Euclidean geometries.