The line is Hamlet's, spoken to his friend Horatio in Act I, scene 5, right after he speaks with his father's ghost: "There are more things in heaven and earth, Horatio, than are dreamt of in your philosophy." Portuguese translations made it proverbial with an extra adjective, "our vain philosophy" (nossa vã filosofia).
The adjective "vã" ("vain") is a translator's addition: it does not exist in the original. The established Portuguese version also has other small differences, such as "between heaven and earth" in place of "in heaven and earth".
The line answers Horatio's astonishment. Hamlet has just made his two friends swear silence on the sword, and the ghost's voice cries "Swear!" from beneath. Hamlet speaks to it as to a comrade: "Are you there, old fellow?" Horatio says: "By day and by night, this is too strange!" And Hamlet replies: "Then, as to a stranger, give it a welcome," before saying the line.
Soon after, he warns his friends that he may find it convenient, from now on, to put on an eccentric conduct, and near the end of the scene he says the lines: "The time is off its hinges. Oh, cursed fate, that I was born to set it right!"