TITLE: Difference between a type and a set
QUESTION [11 upvotes]: I've been trying to understand this distinction for a while, buts its still not making sense to me.
Originally, I thought the distinction between type and set was as follows.

The relationship between any two sets is already determined. For example, given sets $A$ and $B$, either its the case that $A \subseteq B,$ or else its the case that $A \not \subseteq B.$ Along a similar vein, we can always take the union of two sets $A$ and $B$, and we always have $A \subseteq A \cup B.$ Furthermore, in a set-theoretic foundation, no function needs a codomain.
On the other hand, types "float free" in the universe, and are only related by the functions between them. Thus, we cannot write $A \cup B$ if $A$ and $B$ are types, although we can form the sum type $A + B,$ which is basically a disjoint union. Furthermore, in a type-theoretic foundation, every function needs to have a codomain.

So, that's what I used to think.
However, for a variety of reasons, the above paragraph is completely wrong! For example, there are set-theoretic foundations in which sets float free, functions have codomains, etc. (e.g. ETCS).
So now I'm just completely confused.
Can someone explain to me, once and for all, what the difference is?

REPLY [3 votes]: For a philosopher trying to get an initial handle on the conceptual difference between ZFC sets, ETCS sets, and types, Shulman's blog post is useful: https://golem.ph.utexas.edu/category/2013/01/from_set_theory_to_type_theory.html
The following quotes from his post speak to the issue. The cartoon version of the distinction is that ZFC is a propositional system, ETCS is an ontological system, and MLTT is an operational system.  (Zhen Lin's answer is less cartoonish.) The bold indicates my emphasis, not Shulman's.

The basic objects of ZFC and ETCS are both called “sets”, but they behave so differently that it can be confusing to use the same name for both. For clarity, in this post I’ll call them material-sets (for ZFC) and structural-sets (for ETCS). There are a lot of differences between the two, but personally, I think one very important one is that although both kinds of sets have elements, their manner of “having elements” is different.
If $X$ is a material-set, then for any other thing $A$, we can ask whether $A\in X$. The statement $A\in X$ is a proposition in the logician’s sense: something which can be either true or false, which can be assumed as a hypothesis and proven as a conclusion, negated, combined with other propositions using conjunction, disjunction, and implication, quantified over, and so on (...)
By contrast, if $X$ is a structural-set, then there are some things which by their very nature are [given as] elements of $X$ (...) $A\in X$ should not be considered as a proposition (...).
if $\Bbb R$ is a structural-set, then the quantifier “for all $x\in \Bbb R$” is an atomic operation of logic; (...) structural-set theory matches mathematical practice more closely (...) The quantifier “for all $z\in \Bbb C$” is naturally treated as atomic, with $z$ being given as a complex number, rather than as an arbitrary thing with the additional hypothesis that it happens to be a complex number.
(...) power sets seem more natural materially. (...) natural numbers seem more natural structurally. (...) I don’t think that function sets are particularly natural in either case. 
(...) we ought to seek a foundational system which contains structural-sets and their elements as basic things, but also subsets of these which behave like material-sets; contains both the structural quantifier-bounding membership and the material membership proposition; and allows elements of structural-sets to have some internal structure, but doesn’t force them to always have a particular kind of internal structure. The elements of a structural-set should have different structure depending on what that structural-set is: they may be material-sets, or functions, or ordered pairs, or natural numbers.
  In fact, such a foundational system already exists: it is called Martin-Löf dependent type theory (MLTT). (...) Instead of “structural-sets” we speak of types. (...)
In MLTT, we have many different ways to construct (or “form”) types, and each type-forming operation comes with rules specifying what the elements of that type are and how they behave.