First, you have to set up two equations.
X + y = 45
18x + 24y = 1,050
To do this, you have to solve for an exponent by cancelling out the other one. To solve for y, you multiply everything in the first equation by -18.
-18x -18y = -810
This cancels out the x and leaves you with 6y = 240. Divide both sides by 6 and you have the number of Yankee hats that were bought.
Then, you have to substitute 40 for y in the first equation and solve for x.