in your example you have completely ignored prime factorization.
suppose we write a number as a list of exponents n=(e1,e2,e3,...) where only finitely many ei's are non-zero, and so that e1 is the exponent of 2, e2 is the exponent of 3, and en is the exponent of the nth prime.
in your example 12 = 2^2*3^1= (2,1,0,0,...) and 18 = 2^1*3^2= (1,2,0,0,...)
Now, to find the lcm we compare each position and choose the larger (or equal) exponent.
lcm(12,18) = (2,2,0,0,...) = 36
Similarly, to find the gcd we compare each position and choose the smaller (or equal) exponent.
gcd(12,18) = (1,1,0,0,...)= 6
Now 12*18 = (2,1,0,0,...)*(1,2,0,0,...)=(2+1,1+2,0,0,...)
When we multiply the lcm*gcd, we are still going to add the same exponents at the same position, the only difference could possibly be the order which they are added depending on which is larger. Since addition of integers is commutative, this does not matter.
Another example for clarification:
28 = 2^2*7^1 (2,0,0,1,0,0,...)
20 = 2^2*5^1 (2,0,1,0,0,...)
lcm(28,20) = (2,0,1,1,0,0,...)
gcd(28,20) = (2,0,0,0,...)
So each exponent from all primes in both numbers have been accounted for exactly once so the product lcm*gcd is the same as 28*20.