I know weil is a conjunction and wegen is a preposition, but both reply the question "warum". I would like to know in which conditions they must be used.
Weil is because, wegen is because of.
Warum ist der Boden nass? (Why is the floor wet?)
Weil es geregnet hat. (because it rained.)
Warum bleiben Sie zu Hause? (Why do you stay at home?)
Wegen des schlechten Wetters. (Because of the bad weather.)
The usage of these words is not limited to these meanings and examples in my answer.
As you have already noticed yourself, "weil" is a conjunction and "wegen" is a preposition. The answer lies there whithin; You simply use these words as you would with any other of the same class of words.
use "weil" to connect sentences, e.g:
Ich lache, weil ich froh bin
an use "wegen" before nouns or pronouns, e.g:
Ich weine, wegen des sauren Weines
And both together in one esentence:
Ich beklage mich nicht, weil er so blöd ist, sondern wegen seiner Dummheit
Since you seem to want to know in what circumstances you can't paraphrase a phrase containing wegen with a sentence containing weil:
Wegen, being, as you know, a preposition, and therefore only needing a noun afterwards, allows you to be much more ambiguous than weil, which always needs a complete sentence, i.e. usually at least a noun and a predicate. Thus, you can easily substitute weil das Wetter schlecht war for _wegen des schlechten Wetters in
Ich konnte nicht kommen, weil das Wetter schlecht war.
But wegen allows you to be less specific:
Ich konnte wegen eines Unfalls nicht kommen.
This allows you, for example, to conceal whether it was you that had the accident or someone else entirely.
An example would be the sentence you often hear in the underground:
Wegen eines technischen Problems kommt es zu unregelmäßigen Abfahrtszeiten.
Here, the speaker deftly manages to avoid explicitly saying something like
Weil wir ein technisches Problem haben, kommt es zu unregelmäßigen Abfahrtszeiten.
which would sound much worse, because it explicitly links the operating company and the techincal problem.
I will contribute a very obscure additional observation:
In everyday language of the most negligent fashion you may sometimes hear things like this:
Ich konnt' nich' zur Tür weil, der Pudding.
which in standard German would be:
Ich konnte nicht zur Tür kommen wegen des Puddings.
(Imagine somebody cooking a pudding, the door bell rings, but he cannot go and open the door because he has to continue stirring the milk so it does not burn. Sorry for the prosaic example.)
There is a slight pause betweeen 'weil' and what follows. Therefore the comma here.
Of course, this is pure oral usage. You never will write this.
Now, even more attentive users of German will find the 'correct' way of expressing it too circumstantial, and therefore even they would tend to slur the words.
Ich konnt' nich' zur Tür wegm Pudding. [or wegen-m, both from 'wegen dem']
Again, that's pure oral usage.