What you do is fill up the 3 gallon container.
Pour all that water into the 5 gallon container.
Now fill up your 3 gallon container again.
Pour that into the previous 5 gallon container.
Since you already put 3 gallons in the 5 gallon container, you can only put 2 more.
So fill the 5 gallon until it is full.
That would leave you with 1 gallon remaining in the 3 gallon container.
Math wise, in total what you did was try to pour 6 gallons into the 5 gallon container. Since it could only hold 5, you have 1 gallon remaining.