Both units come from the inch
A cubic foot is 12 × 12 × 12 = 1,728 cubic inches, and a US gallon was defined in 1707 as exactly 231 cubic inches. Divide one by the other and a cubic foot is 7.480519… US gallons — an exact ratio, not a measurement. In litres a cubic foot is 28.3168, and an imperial gallon (4.54609 L) fits 6.2288 times, which is why UK and US tank capacities never quite agree.
Where this comes up
Refrigerators and freezers are rated in cubic feet (a 20 ft³ fridge holds 150 US gallons of air). Water meters in many US cities bill in hundreds of cubic feet: one CCF is 748 gallons. An aquarium 4 ft × 1 ft × 1.5 ft is 6 ft³, about 45 gallons. For a pool, multiply length × width × average depth in feet, then convert here.