When we actually try reading a file from HDFS, we know for a fact that it comes back in the right sequence. Ergo, the HDFS namenode knows how to find those fragments stored in different data nodes.
Now how
exactly it does this in code requires one to know HDFS internal implementation, and I don't know them. Perhaps you can start from
DFSInputStream and see what data structures it uses to understand how it works.