The existence of beddings and abundant interlayers in sandstone reservoirs severely hinders the exploration and extraction of sandstone gas reservoir resources. In this study, layered heterogeneous sandstone cores were constructed to explore the impact of sandstone layered heterogeneity on gas-water flow, and the gas-water displacement process was imaged by using X-ray tomography. The changes in the fluid distribution were studied with the help of imaging data. After displacement began, under the influence of heterogeneity, fluid entered the dominating channel made up of big pores preferentially, which had a poor displacement effect in the low permeability layer. In the low permeability layer, a large amount of residual gas was stranded downstream in dead-end corners because there were no efficient seepage channels. Using the residual gas's shape and distribution properties, three categories & horbar;network, multiple, and singlet & horbar;were divided. The results show that, with the succession, the large-volume connected network residual gas dominant in pore space was dispersed into smaller multiple residual gas; due to poor connection, the initial percentage of the network residual gas in the low permeability layer of the core was lower than the initial percentage of the network residual gas in the high permeability layer, and the extent of the network residual gas' dispersion was also less than in the core's high permeability layer; after increasing back pressure, due to the difficulty in retaining multiple residual gas in the core's high permeability layer, in comparison to the core's low permeability layer, the core's high permeability layer had a larger fraction of singlet residual gas. Only multiple and singlet residual gas remained in the pore space. This study offers fundamental knowledge for the investigation of the gas-water flow law in heterogeneous sandstone, which may help to further enhance the gas reservoir recovery in heterogeneous sandstone.