FIFO (First-in-First - out) assumes that units purchased at the beginning of the accounting period are sold first. From that we can see that the ending inventory is the cost of the last units purchased but not sold. For example, assuming that a company sells 100 units.

The beginning inventory was 10 units @ $3 per unit, total cost = (10 *$3) = $30

Purchased 40 units @ $4 , total cost = (40 * $4) $160

Purchased 70 units @ $5, total cost = (70*$5) = $350

Of the 100 units that were sold, therefore, 10 units were purchased at $3, 40 units were purchased at $4, and 50 units were purchased at $5. Therefore what is remaining in inventory is the remainder of the 70 units purchased at $5 (which = 70 - 50 = 20 units)

The cost of ending inventory, hence = 20*$5 = $100