It’s not important whether the last, penultimate, fifth-to-last or any other carbon’s oxygen reacts with the carbonyl group to form a cyclic structure. The only thing that matters is the ring size you generate.
A concept called Baeyer strain exists which was introduced by Adolf von Baeyer in the 19th century. It bases on the fact that the tetrahedral angle is approximately $109.5^\circ$ but a the bonds in e.g. a three-membered ring must feature bond angles of $60^\circ$ due to the laws of geometry. The difference in bond angles induces strain.
This Baeyer or angle strain is particularly prevalent in small rings (three- and four-membered) and semi-large rings (eight- to twelve-membered). However, five- and six-membered rings are notable exceptions; both are almost unstrained. For five-membered rings this is immediately obvious since a pentagon has an inner angle of $108^\circ \approx 109.5^\circ$. For six-membered rings, one needs to account for the chair conformation, which allows perfect tetrahedral angles.
Since five- and six-membered rings are rather stable compared to other ring sizes, monosaccharines will primarily form these ring sizes. If both are possible, a five-membered ring is often favoured kinetically (i.e. it forms more rapidly than a six-membered one) but the six-membered ring is favoured thermodynamically (i.e. it has a lower overall energy). For many sugars, six-membered rings overwhelmingly dominate the species found in solution: in glucose solutions, the five-membered (furanose) forms are only present in less than $1~\%$ of all possible forms.
However, if you take erythrose or threose, six-membered rings are impossible since only five atoms can from the chain. Thus, these sugars will exclusively form furanose structures, never pyranoses (six-membered rings).
When determining which oxygen attacks, start counting on the carbonyl carbon and try counting to 6, landing on an oxygen. If that is possible, a pyranose form (six-membered ring) is very likely. If not, try again attempting to reach an oxygen on 5. If that is possible, furanose forms will form. Other ring sizes can typically be disregarded as they only account for minute quantities of the solute distribution.