The is a certain way of talking where you can end just about anything you say in わけ.
What is the sentence structure for this way of talking?
While sawa's answer does cover the basic construction rules, it's definitely worth it to go over the different use cases of わけ. Grab a comfy chair and your favorite beverage, because this is a long one.
The best and most complete analysis I've found of this use of わけ is in this 2001 paper by Atsuko Yokota:
文末【ぶんまつ】表現【ひょうげん】「わけだ」の用法【ようほう】 : 「はずだ」「ことになる」との比較【ひかく】
It's freely available as a PDF (yay!) but it's entirely in Japanese (hrm), so the best I can give you here is a summary of the five (yes, five) distinct uses of わけだ which Yokota lists.
Diagrammatically, Yokota charts out the first four uses of わけだ in her paper. Here is a clarified and prettified version:
The common thread in this type of sentence is that a known fact Y is used as the logical basis for a conclusion Z:
波【なみ】がずいぶん荒【あら】いですね。今日【きょう】は船【ふね】が出【だ】せないわけですか。
Y (known): The water is really rough today, isn't it?
Z (conclusion): As a result, will we not be able to put out to sea today?
時差【じさ】が四時間【よじかん】あるから、日本時間【にほんじかん】のちょうど正午【しょうご】につくわけだ。
Y (known): The time difference is four hours, so
Z (conclusion): based on this we'll arrive right at noon Japan time.
体重【たいじゅう】をはかったら52キロになっていた。先週【せんしゅう】は49キロだったから、一週間【いっしゅうかん】で3キロも太【ふと】ってしまったわけだ。
Y (known): When I weighed myself I was at 52 kilos. Last week I was 49 kilos, so
Z (conclusion): this means I gained a whole three kilos in one week.
This is like the first use, only in the opposite direction. We take some known fact Y, and use わけ to mark X, the reason for or cause of Y:
学校【がっこう】の中【なか】が静【しず】かですね。――あ、冬休【ふゆやす】みに入【はい】ったわけですね。
Y (known): It's quiet in the school, isn't it?
X (reason): Ah, it's because we're in winter break now, right?
今年【ことし】の米【こめ】のできが良【よ】くなかった。冷夏【れいか】だったわけだ。
Y (known): This year's rice crop was poor.
X (reason): This is due to the cool summer we had.
波【なみ】がずいぶん荒【あら】いでしょう。台風【たいふう】が近【ちか】づいているわけです。
Y (known): See how the water is really rough?
X (reason): It's because there's a typhoon approaching.
This is a little more complicated than the first two uses. Essentially, we again start with some known fact Y (which may be unspoken). An answer for why Y is true is presented as X, and this leads to the conclusion that X naturally yields Y:
フランスで中学【ちゅうがく】まで行【い】かれたのですか。道理【どうり】で、フランス語【ご】が流暢【りゅうちょう】なわけですね。
Y (known, unspoken): You are fluent in French.
X (why is Y true?): You were in France through junior high?
Y (acknowledged): No wonder you're fluent in French. (acknowledging that X naturally yields Y)
あかないわけです。かぎが違【ちが】っているのですから。
Y (known): It won't open. (Listener: Why not?)
X (why is Y true?): The keys are different. (Listener: Ah, so that's why.)
夫【おっと】:隣【となり】の鈴木【すずき】さん、退職【たいしょく】したらしいよ。
妻【つま】:そうか。だから平日【へいじつ】の昼間【ひるま】でも家【いえ】にいるわけだ。
Y (known by wife, unspoken): Mr. Suzuki is at home even during the middle of the day on weekdays.
X (why is Y true?): Husband: I heard that Mr. Suzuki next door resigned from his job.
Y (acknowledged): Wife: Ah, so that's why he's at home even during the middle of the day on weekdays.
This is one of the simplest uses of わけ. Here, a known fact Y is restated or presented from a different angle as Yʹ. Often you'll find this use of わけ paired with つまり:
彼女【かのじょ】の父親【ちちおや】は私【わたし】の母【はは】の弟【おとうと】だ。つまり彼女【かのじょ】と私【わたし】はいとこ同士【どうし】なわけだ。
Y: Her father is my mother's younger brother.
Yʹ: In other words, she and I are cousins.
山田【やまだ】さんは韓国【かんこく】の領事館【りょうじかん】勤務【きんむ】になった。つまり彼【かれ】は出世【しゅっせ】したわけだ。 (example adapted from this page)
Y: Mr. Yamada got a job at the South Korean consulate.
Yʹ: Essentially, he's moved up in the world.
Unfortunately, there has to be a catch-all category, since わけ gets used kind of like a sentence-final particle without any significant effect on the meaning of the sentence. Even Yokota admits that "speakers often use [わけ in this way] unconsciously."
こうして二人【ふたり】は結婚【けっこん】して、幸【しあわ】せに暮【く】らしたわけです。
And so the two married and lived happily.
This わけ doesn't fit nicely into any of the four preceding categories. You might think it falls under #1 (a conclusion based on some known fact), but this sentence isn't a conclusion of anything; it's merely a statement of some known fact, without any connection. We could, in fact, remove わけ entirely, without affecting the meaning, by replacing 暮【く】らしたわけです with 暮【く】らしました.
Inside this category is the use of わけ to set up a context or prologue for a succeeding statement. We find ~のだ being used in a similar way:
わたしは国史【こくし】を専門【せんもん】にしているわけですが、わたしのような文献【ぶんけん】を扱【あつか】う者【もの】の立場【たちば】からすれば、もっと史料【しりょう】を大切【たいせつ】にすべきではないかと思【おも】うんです。
I specialize in history, and from the standpoint of someone like me who handles literature, I think we should value historical sources more.
Here, わけ marks a prologue ("I specialize in history") and provides the foundation for the following statement: it informs the listener of how qualified the speaker is to make such a statement. わけ could be replaced by の (ん) in this example.
The rules for わけ follow the normal rules for forming a subordinate clause before a noun: the clause must end in a plain form, with the exception that (plain present) な-adjectives connect to わけ with な, and (plain present) nouns can connect with either な or (less commonly) の:
行【い】くわけだ/行【い】ったわけだ/行【い】かないわけだ/行【い】かなかったわけだ
優【やさ】しいわけだ/優【やさ】しかったわけだ/優【やさ】しくないわけだ/優【やさ】しくなかったわけだ
静【しず】かなわけだ/静【しず】かだったわけだ/静【しず】かではないわけだ/静【しず】かではなかったわけだ
証拠【しょうこ】なわけだ/証拠【しょうこ】だったわけだ/証拠【しょうこ】ではないわけだ/証拠【しょうこ】ではなかったわけだ
Often the speaker will use という to "wrap up" the preceding clause (or sentences) and use it to modify わけ (というわけだ). This doesn't change the usage, and the rules for using という are the same as those for the use of と to mark a quote or thought. This use of という is often used at the beginning of a sentence as a transition:
というわけで、…
So with that, …
This is especially common in TV or radio shows when the host needs to press the show forward or move on to the next topic. The という wraps up the preceding statements into わけ and essentially means, "Now that all this has been said, let's move on." というわけで has the following colloquial forms:
ちゅうわけで (a slur of というわけで)
てなわけで (using て for という, but as to where the な comes from, your guess is as good as mine)
ということで is also used as a transition with a meaning similar to というわけで.
There are at least four negative forms of わけ, each with slightly different meanings. One is using わけだ after a negative clause, which shows up in the examples here. This answer is getting too long to cover these, so I will simply list the other three here and wait for those interested in more complete explanations to ask a question:
わけ
is a noun meaning 'reason', but it might be better translated as 'circumstances' as Matt comments. When you put it at the end of a sentence, you are turning that sentence into an appositive clause modifying わけ. When the predicate is an adjectival noun (also called na-adjective), you need to change the ending into the adnominal ending (-な). A word for word translation will be "(there is) reason that...", or "the circumstances were that...".
In actual usage, it does not mean much, but is used when you want to establish intermediate steps for explaining something.
彼がそこにいた。
'He was there.'
彼がそこにいたわけ。
'{Reasonably/The circumstances were that/Now/Actually}, he was there.'彼は静かだ。
'He is quiet.'
彼は静かなわけ。
'{Reasonably/The circumstances were that/Now/Actually}, he is quiet.'