Using a systematic method, based on considering all possible hydrogen bond connections between two melamine molecules, and ab initio density functional theory (DFT) calculations, we consider the possible planar superstructures that the molecules can form in two dimensions. This is relevant to the assembly of melamine on flat metal surfaces with a small lateral corrugation of the molecule-surface interaction energy. The structures considered include small clusters as well as periodic structures, such as one-dimensional filaments and two-dimensional monolayers. Then, the interaction of melamine structures with the Au(111) surface is considered in detail to elucidate the possible effect of the surface on the formed structures, including the influence of the van der Waals interaction, which is not taken into account in DFT-based methods. The problem of commensurability between the lattices of the gas-phase monolayer and of the substrate is also discussed.