Shouldn't the angle of incidence always equal that of emergence? In the case of a refracted light ray in, say, a glass cuboid, wouldn't the angle of incidence always equal that of refraction regardless of the dimensions (of the glass slab) as an application to Fermat's path of least time principle? I have seen problems where it doesn't and I'm curious where that doesn't apply or if my understanding is flawed.
 A: Fermat's principle
The Fermat's principle of least time states that the light takes a path between two points such that the traveling along the path takes the least time.

Refractive Index
Light travels slower in a medium. For example, light travels at $2\times 10^8 ms^{-1}$ in glass. Refractive index is a number which desribes the optical properties of the material.
It is defined as:
$$\mu = \frac{c}{v}$$
where $c$ is the speed of light in vacuum and $v$ is the speed of light in the medium.

Building an intuition

Without the loss of generality, we will assume that light travels slower in medium blue than medium gray. 
Let us compare the purple path and the brown path. In the purple path, light travels most of the distance in medium blue. In the brown path, light travels most of the distance in medium gray. As light travels slower in medium blue, it is a good guess that the total time taken for the light to take a particluar path is smaller in the case of brown path. 
It would be initially be conviencing to say that to travel in the least possible time, we should minimize the distance the light travels in medium blue. This case is represented by the line coloured brown in the diagram.
However, this is not the best possible case. Compare the yellow line with the brown line; isn't the total distance light travels in brown path larger than the total distance the light travels in the yellow path?
Yes, that's what's making the problem complicated. It is s tug of war between the total distance the light has to travel and reducing the amount of distance the light travels in the slower medium.
Consider the green ray. This ray travels in a straight line from point $A$ to point $B$. Ask yourself if increasing the total distance the light travels and reducing the distance the light travels in the blue medium would take lesser time for light to travel? Yes, it is better always (unless light travels at the same speed in both the mediums).
As we try to reduce the distance light travels in the medium blue at the cost of increasing the total distance the light travels, we will reduce the total time taken up to a point. The path where it takes the least time is the path which Snell's law (law of refraction) says. From this point, if we further try to reduce the distance the light travels in the medium blue, the increase in length will prove to more costly than the reduction in time taken for the light to travel in medium blue.
By writing the equation for the total time light takes to travel, we can use math tools (such as calclus) to calculate the path where light takes the least time to travel.

Deriving Snell's law using Fermat's principle

Let the length of $AC$ be $h_1$
Let the length of $DB$ be $h_2$
Let the length of $CO$ be $x$
Let the length of $CD$ be $l$
Let the angle of incidence ($\angle AON$) be represented by $i$
Let the angle of refraction ($\angle BON`$) be represented by $r$
Let $n_1$ be the refractive index of the medium coloured in gray
Let $n_2$ be the refractive index of the medium coloured in blue
$$$$
Fermat's principle states that the light takes the path where it takes the least amount of time to travel. We will first write down the equation for the distance traveled by light in the two medium.
$$AO = \sqrt{h_1^2 + x^2}$$
$$OB = \sqrt{h_2^2 + (l-x)^2}$$
As the refractive index of the two mediums are different, light travels at different speeds in the medium. The time taken by light to travel in the two mediums can be caculated as follows:
$$t_{n_1} = \frac{\sqrt{h_1^2 + x^2}}{v} = \frac{\sqrt{h_1^2 + x^2}}{\frac{c}{n_1}} = \frac{n_1\sqrt{h_1^2 + x^2}}{c}$$
$$t_{n_2} = \frac{\sqrt{h_2^2 + (l-x)^2}}{v} = \frac{\sqrt{h_2^2 + (l-x)^2}}{\frac{c}{n_2}} = \frac{n_2 \sqrt{h_2^2 + (l-x)^2}}{c}$$
$$total\_time = t_1 + t_2 = \frac{n_1\sqrt{h_1^2 + x^2}}{c} + \frac{n_2 \sqrt{h_2^2 + (l-x)^2}}{c}$$
We want to minimize $total\_time$, we can obtain the minimum condition using differential calculus.
$$\frac{d({total\_time})}{dx} = \frac{n_1 x}{c\sqrt{h_1^2 + x^2}} - \frac{n_2 (l-x)}{c\sqrt{h_2^2 + (l-x)^2}}$$
For the minimum value, $\frac{d({total\_time})}{dx}$ must be zero.
$$\frac{n_1 x}{c\sqrt{h_1^2 + x^2}} - \frac{n_2 (l-x)}{c\sqrt{h_2^2 + (l-x)^2}} = 0$$
$$\frac{n_1 x}{c\sqrt{h_1^2 + x^2}} = \frac{n_2 (l-x)}{c\sqrt{h_2^2 + (l-x)^2}}$$
$$n_1 \sin i = n_2 \sin r$$
A: If the ray emerges from the face parallel to the one at which it was incident, and the refractive indices at the two boundaries are the same, then the emergent ray will be parallel to the incident ray. 
This is true not only for a cuboid of a single material but also for a complex slab of layered materials, provided that the 1st and last layers are the same material. The quantity $\mu\sin\theta$ is a constant as the ray traverses the slab.

The emergent ray is not parallel to the incident ray if it is refracted so that it emerges from a side face.
I cannot comment on the cases you've seen where this does not happen, because you have not provided any details.
A: In the denser medium the light travels slower. That means the temporaly shortest path is one that spends more time in the less dense medium - it is bent. 
The shortest path from the medium boundary to the destination point is the normal path.
The light path develops a bend at the boundary either towards or away from that, according to Fermat's principle.
The bend alters the length of the paths in both media (as a different point of intersection for the incoming light will need to be selected).
