When does a space admit a flat metric?
The octagon with edges identified appears to be different because you cannot tile the entire flat (Euclidean) plane with octagons in such a way that everything is OK when you cross the "border" and move to the adjacent octagon.
Instead, you can tile the hyperbolic plane with octagons in the correct way. So the genus-2 surface can inherit the negative-curvature metric of the hyperbolic plane. Pictures: Wikipedia: Order-8 octagonal tiling
Let $M$ be a manifold and $G$ a discrete group acting freely and proper discontinuously, then $M/G$ is a manifold. If $g$ is a Riemannian metric on $M$, then it descends to a Riemannian metric on $M/G$ if and only if the group $G$ acts by isometries; to be precise, by 'descends to a Riemannian metric on $M/G$', I mean there is a Riemannian metric $g'$ on $M/G$ such that $g = \pi^*g'$ where $\pi : M \to M/G$ is the quotient map. To see how to construct $g'$ from $g$, see this answer where I show that the standard metric on $\mathbb{R}^n$ descends to the torus $\mathbb{R}^n/\mathbb{Z}^n$; here the group $G = \mathbb{Z}^n$ is acting by translations which are isometries of the usual metric on $\mathbb{R}^n$.
If $(M, g)$ is flat, and $g$ is complete, then its universal cover is isometric to $\mathbb{R}^n$ with its standard metric, so every complete flat manifold is a quotient of $\mathbb{R}^n$ with its standard metric by a discrete group of isometries which acts freely; note, every discrete group of isometries of $\mathbb{R}^n$ acts properly discontinuously. If in addition $M$ is compact, then the group action is cocompact; a discrete group of isometries which acts cocompactly is called a crystallographic group, if it also acts freely, it is called a Bieberbach group; note, a Bieberbach group can equivalently be defined as a torsion-free crystallographic group (this is the usual definition). Bieberbach showed that an $n$-dimensional crystallographic group has a finite index subgroup isomorphic to $\mathbb{Z}^n$, from which it follows that every compact flat manifold is finitely covered by a torus.
Note that not every subgroup of isometries of $\mathbb{R}^n$ gives rise to a flat manifold as it may not act freely on $\mathbb{R}^n$. For example, every isometry of $\mathbb{R}^2$ is a composition of reflections, rotations, and translations; the first two transformations have fixed points, so they don't act freely on $\mathbb{R}^2$. If the subgroup does not act freely, the quotient will be a flat orbifold instead of a flat manifold. Moreover, every flat orbifold arises this way, see Theorem $13.3.10$ of Foundations of Hyperbolic Manifolds (second edition) by Ratcliffe. In particular, a quotient of $\mathbb{R}^n$ by a crystallographic group which is not a Bieberbach group gives a compact flat orbifold which is not an manifold.
You can use this idea to put a flat metric (in a sense) on any surface obtained by identifying the edges of a polygon in pairs. But the metric won't be smooth unless whenever you have a set of vertices being identified, the angles at those vertices add up to exactly $360^\circ$. It works for the square because you have four $90^\circ$ angles meeting at a point. To see what goes wrong, cut a regular octagon out of a piece of paper, then tear off eight pieces containing the eight vertices and try to tape them all together so that all the vertices meet at a point. You can do it, but you'll get a shape that can't be flattened out without a lot of creasing.
Here's an example of a hexagon that works:
The vertices labeled A are all identified with each other, as are the vertices labeled B. Since the angles all measure $120^\circ$, each set of three angles fits together to make a smooth flat surface. (The resulting surface is homeomorphic to the torus, as it must be because it has a flat metric. It's an interesting exercise to prove this directly by cutting and pasting.)
The reason you can put a smooth hyperbolic metric on a surface of genus $n=2$ or more is that as long as $n\ge 2$, it's possible to find a regular $4n$-sided geodesic polygon in the hyperbolic plane whose angles are all exactly $360^\circ/4n$, so they all fit together to make a smooth surface with a hyperbolic metric.