Raycast returns a normal which is perpendicular to the surface hit. If you place based on the position of the part (center), offset by size of the parts, oriented based on the surface normal, you should get perfect placement.
if you are using blocks, it is easy. Say the blocks are size 1, then from the middle to the side is 0.5, and then again another 0.5 to the middle of the next block. For irregular shapes… my brain hurts!
You could probably procedurally figure this out for every building block, but it might be easier to figure these values out and add them as attributes to the block (or use OOP, etc).
There are a few threads on this sort of calculation. This one tries to get the size of the surface, which you are going to need for irregular shapes, but to get there, it describes how to calculate the distance to the surface. With weird size shapes (walls) you are also going to need to figure out how to align with the edge of the adjoining part.