How to do I go about welding parts together depending if their surfaces are touching?

If your parts are all rectangular and only rotated in 90-degree increments, something like this would work:

That thread has some more discussion as well.