We have performed direct numerical simulations of turbulent flows in a square duct considering a range of Reynolds numbers spanning from a marginal state up to fully developed turbulent states at low Reynolds numbers. The main motivation stems from the relatively poor knowledge about the basic physical mechanisms that are responsible for one of the most outstanding features of this class of turbulent flows: Prandtl's secondary motion of the second kind. In particular, the focus is upon the role of flow structures in its generation and characterization when increasing the Reynolds number. We present a two-fold scenario. On the one hand, buffer layer structures determine the distribution of mean streamwise vorticity. On the other hand, the shape and the quantitative character of the mean secondary flow, defined through the mean cross-stream function, are influenced by motions taking place at larger scales. It is shown that high velocity streaks are preferentially located in the corner region (e.g. less than 50 wall units apart from a sidewall), flanked by low velocity ones. These locations are determined by the positioning of quasi-streamwise vortices with a preferential sign of rotation in agreement with the above described velocity streaks' positions. This preferential arrangement of the classical buffer layer structures determines the pattern of the mean streamwise vorticity that approaches the corners with increasing Reynolds number. On the other hand, the centre of the mean secondary flow, defined as the position of the extrema of the mean cross-stream function (computed using the mean streamwise vorticity), remains at a constant location departing from the mean streamwise vorticity field for larger Reynolds numbers, i.e. it scales in outer units. This paper also presents a detailed validation of the numerical technique including a comparison of the numerical results with data obtained from a companion experiment.