The overflow flag V records signed overflow: the twos complement result is outside the signed range, so its bits read as the wrong number.
The hardware is one XOR gate on the top two carries of the adder:
V = Cₙ ⊕ Cₙ₋₁
Cₙ is the carry out of the sign bit and Cₙ₋₁ is the carry into it. In a 4-bit adder V = C4 ⊕ C3; in an 8-bit adder, V = C8 ⊕ C7.
An equivalent form uses only sign bits: overflow happens when both operands have the same sign and the result has the other. For 4 bits, V = . For a subtraction, use the sign of B as the adder sees it, after the XOR.
V is for signed numbers only. For unsigned arithmetic, check the carry flag instead. The two flags are independent, and all four combinations of C and V occur.
Common wiring mistakes in lab ALUs: using the carry-out as V, keeping C4 ⊕ C3 after widening a design to 8 bits (that checks the middle of the number, not the sign bit), and judging a subtraction by the sign of the original B.